Skip to content

Adding lists of subtypes together #2383

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
alanhdu opened this issue Aug 14, 2018 · 3 comments · Fixed by #8293
Closed

Adding lists of subtypes together #2383

alanhdu opened this issue Aug 14, 2018 · 3 comments · Fixed by #8293

Comments

@alanhdu
Copy link
Contributor

alanhdu commented Aug 14, 2018

Using mypy==0.620 and Python 3.6.6, the following code:

from typing import List, Optional

x: List[Optional[int]] = [1, 2, 3]
[None] + x

fails with:

a.py:4: error: Unsupported operand types for + ("List[None]" and "List[Optional[int]]")

This is presumably because lists are invariant w.r.t their generic parameter, and the declration requires a List of the same type

def __add__(self, x: List[_T]) -> List[_T]: ...
.

I don't know what the right solution here is -- can we just replace the x: List[_T] with x: List[_T_co] and make it covariant? Or doe we need another typevar _U = TypeVar("U", bound=_T) or something?

@JelleZijlstra
Copy link
Member

This is close to python/mypy#5492. I found a fix in #2404.

ilevkivskyi pushed a commit that referenced this issue Nov 8, 2018
yedpodtrzitko pushed a commit to yedpodtrzitko/typeshed that referenced this issue Jan 23, 2019
@wy193777
Copy link

#2404 has been reverted. Do we have a solution for this issue?

@tamuhey
Copy link

tamuhey commented Apr 20, 2021

+1 from microsoft/pyright#1777

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

Successfully merging a pull request may close this issue.

4 participants