为什么 Sequence 是 mypy 中 + 不支持的操作数类型?

cdw*_*son 5 python typing mypy

mypy给出的错误Sequence[str]不是该运算符支持的操作数类型+

# test.py

from typing import Sequence


def test(x: Sequence[str], y: Sequence[str]) -> Sequence[str]:
    return x + y

Run Code Online (Sandbox Code Playgroud)
$ mypy test.py
test.py:5: error: Unsupported left operand type for + ("Sequence[str]")
Found 1 error in 1 file (checked 1 source file)
Run Code Online (Sandbox Code Playgroud)

pytype 给出了类似的错误:

$ pytype test.py 

[...]

  No attribute '__add__' on Sequence[str] or '__radd__' on Sequence[str]

[...]
Run Code Online (Sandbox Code Playgroud)

为什么 是Sequence[str]不受支持的操作数类型+

Sam*_*ord 4

根据文档,序列不一定实现__add__

https://docs.python.org/3/glossary.html#term-sequence

不支持串联的一个示例Sequencerange.

  • 那么,对于支持“+”连接的通用序列(接受列表、元组、str)应该使用什么类型提示?(或者我应该问这是一个新问题吗?) (2认同)