Skip to content

Type inference of list comprehension and Optional[t] #1734

Description

@JukkaL

The type of b is incorrectly List[Union[None, int]] instead of List[int] when using --strict-optional:

from typing import Optional, List
a = [] # type: List[Optional[int]]
b = [x for x in a if x]
reveal_type(b)

Generators and other comprehensions are related (not checked them).

Activity

  1. ddfisher commented on Jul 21, 2016

    @ddfisher
    Collaborator

    Rather than being strict optional specific, this is a general problem with Unions:

    from typing import Optional, List, Union
    a = [] # type: List[Union[int, str]]
    b = [x for x in a if isinstance(x, int)]
    reveal_type(b)  # E: Revealed type is 'builtins.list[Union[builtins.int, builtins.str]]'
  2. added a commit that references this issue on Aug 9, 2016
    e2561a8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions