Skip to content

From List[Optional[X]] to List[X] with assert #8822

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
Alberth289346 opened this issue May 14, 2020 · 2 comments
Closed

From List[Optional[X]] to List[X] with assert #8822

Alberth289346 opened this issue May 14, 2020 · 2 comments

Comments

@Alberth289346
Copy link

The trimmed down version of the issue is as follows:

from typing import List, Optional

xs: List[Optional[int]] = [1]
assert all(x is not None for x in xs)
ys: List[int] = xs    # Line 5

gives error:

ex.py:5: error: Incompatible types in assignment
         (expression has type "List[Optional[int]]",
            variable has type "List[int]")

I expected it to work, as it is an extension of the working

x: Optional[int] = 1
assert x is not None
y: int = x    # Works

except I have a list of possibly integer rather than one.

I can likely solve the issue by copying the list one item at a time and doing the check, but xs is a proper result already. I already had the assert all(...) in my original use-case.

mypy 0.670
python 3.7.7

@JelleZijlstra
Copy link
Member

See #4573.

@Alberth289346
Copy link
Author

List being invariant. Good point, I didn't think of that. Thanks.

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