Skip to content

Mypy forgets type information after a nested if-statement. #5378

@PetterS

Description

@PetterS

Mypy forgets type information after a nested if-statement. Small example:

class Base:
    pass


class B(Base):
    def __init__(self):
        self.args = []

class C(Base):    
    def __init__(self):
        self.args = []


def check(x: Base):
    if isinstance(x, (B, C)):

        print(x.args)  # No error here.

        if isinstance(x, B):
            print("B")
        elif isinstance(x, C):
            print("C")

        print(x.args)  # error: "Base" has no attribute "args"

Mypy 0.620 correctly infers that x has a member args for the first print statement, but after the nested if statement, it has forgotten about that and throws an error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions