Skip to content

isinstance(x, (t1, t2)) doesn't change mypy's knowledge of x's type. #1328

Closed
@nathanielherman

Description

@nathanielherman

For example in the following code:

def f(x):
  # type: (object) -> list
  if isinstance(x, (list, set)):
    return [g(i) for i in x]

mypy will complain that an Iterable is expected (it still only sees x as an object). This can be resolved with e.g. a manual cast of x to Union[list, set], but this adds a slight overhead and isn't as clean.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions