Skip to content

Allow heterogeneous concatenation for list #5234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tamuhey opened this issue Apr 20, 2021 · 2 comments
Closed

Allow heterogeneous concatenation for list #5234

tamuhey opened this issue Apr 20, 2021 · 2 comments

Comments

@tamuhey
Copy link

tamuhey commented Apr 20, 2021

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:

class list(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:

class list(MutableSequence[_T], Generic[_T]):
    ...
    def __add__(self, x: List[_S]) -> List[Union[_T, _S]]: ...
@JelleZijlstra
Copy link
Member

See #3183 for some past attempts.

@tamuhey tamuhey closed this as completed Apr 20, 2021
@tamuhey
Copy link
Author

tamuhey commented Apr 20, 2021

Also related: #2383

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants