为什么 Sequence 在 mypy 中是 + 不支持的操作数类型?
Why is Sequence an unsupported operand type for + in mypy?
mypy
给出了一个错误,即 Sequence[str]
不是 +
运算符支持的操作数类型:
# test.py
from typing import Sequence
def test(x: Sequence[str], y: Sequence[str]) -> Sequence[str]:
return x + y
$ mypy test.py
test.py:5: error: Unsupported left operand type for + ("Sequence[str]")
Found 1 error in 1 file (checked 1 source file)
pytype给出了类似的错误:
$ pytype test.py
[...]
No attribute '__add__' on Sequence[str] or '__radd__' on Sequence[str]
[...]
为什么 Sequence[str]
是 +
不支持的操作数类型?
根据文档,序列不一定实现 __add__
:
https://docs.python.org/3/glossary.html#term-sequence
不支持串联的 Sequence
示例是 range
。
mypy
给出了一个错误,即 Sequence[str]
不是 +
运算符支持的操作数类型:
# test.py
from typing import Sequence
def test(x: Sequence[str], y: Sequence[str]) -> Sequence[str]:
return x + y
$ mypy test.py
test.py:5: error: Unsupported left operand type for + ("Sequence[str]")
Found 1 error in 1 file (checked 1 source file)
pytype给出了类似的错误:
$ pytype test.py
[...]
No attribute '__add__' on Sequence[str] or '__radd__' on Sequence[str]
[...]
为什么 Sequence[str]
是 +
不支持的操作数类型?
根据文档,序列不一定实现 __add__
:
https://docs.python.org/3/glossary.html#term-sequence
不支持串联的 Sequence
示例是 range
。