Skip to content

Should Sequence support __add__? #6985

@jamesbraza

Description

@jamesbraza

The Python docs on Sequence mentions it supports concatenation here. The current Sequence (link) doesn't include __add__.

Please see the below code:

from typing import Sequence

# Can add List
list_1: Sequence[int] = [1, 2]
list_2: Sequence[int] = [3, 4]
assert len(list_1 + list_2) == 4

# Can't add range
range_1 = range(5)
range_2 = range(6)
concatenated_ranges = range_1 + range_2

Running this code throws a TypeError on the last line:

Traceback (most recent call last):
  File "/path/to/foo.py", line 11, in <module>
    concatenated_ranges = range_1 + range_2
TypeError: unsupported operand type(s) for +: 'range' and 'range'

Running mypy==0.931 on this I get the below errors:

path/to/foo.py:6:12: error: Unsupported left operand type for + ("Sequence[int]")  [operator]
path/to/foo.py:11:23: error: Unsupported left operand type for + ("range")  [operator]
Found 2 errors in 1 file (checked 21 source files)

I believe mypy is raising these errors per typeshed Sequence not implementing __add__.

So, there are two possibilities:

  • Either: the Python docs should mention __add__ isn't universally implemented for all Sequence
  • Or range should implement __add__ and we should add Sequence.__add__ should be added to typeshed

What do you think?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions