Skip to content

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

Closed
@Alberth289346

Description

@Alberth289346

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

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