Skip to content

Type of variable after isinstance check with 'and' #473

Closed
@spkersten

Description

@spkersten

After a guard with isinstance, mypy should consider the variable to be of that type.

For example:

def f(x: A) -> None:
    if isinstance(x, B) and x.value:
        pass

class A:
    pass
class B(A):
    value = 0

Here, mypy reports that x doesn't have a field value. This can of course be solved with a cast, but that is annoying to type and clutters the code. At least in simple case, mypy could infer that x is of type B in the rhs of the and-statement and in the block following the if-statement.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions