Search code examples
pythonpython-typingmypy

Why is Sequence an unsupported operand type for + in mypy?


mypy is giving an error that Sequence[str] is not a supported operand type for the + operator:

# 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 gives a similar error:

$ pytype test.py 

[...]

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

[...]

Why is Sequence[str] an unsupported operand type for +?


Solution

  • According to the doc, sequences do not necessarily implement __add__:

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

    An example of a Sequence that doesn't support concatenation would be a range.