Skip to content

Allow heterogeneous concatenation for list #5234

Description

@tamuhey

From microsoft/pyright#1777

[0] + ["a"] is not allowed in any type checker (e.g. pyright, mypy) because the type annotation of list.__add__ is defined as follows:

classlist(MutableSequence[_T], Generic[_T]):
...
def__add__(self, x: List[_T]) ->List[_T]: ...

This annotation prohibits heterogeneous concatenation, although it is actually allowed in runtime.
I think the annotation should be as follows to solve this problem:

classlist(MutableSequence[_T], Generic[_T]):
...
def__add__(self, x: List[_S]) ->List[Union[_T, _S]]: ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions