Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
PetterS opened this issue Jul 20, 2018 · 6 comments
Closed

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

PetterS opened this issue Jul 20, 2018 · 6 comments
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal

Comments

@PetterS
Copy link

PetterS commented Jul 20, 2018

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.

@gvanrossum gvanrossum added the bug mypy got something wrong label Jul 20, 2018
@gvanrossum
Copy link
Member

Yeah, this does look like a bug. I somehow thought that the "binder" (the part of mypy that handles this) uses a stack, but here it appears it has popped too much off the stack.

Does this also occur with older mypy versions?

@PetterS
Copy link
Author

PetterS commented Jul 21, 2018

When I encountered this, I had 0.610.

@JukkaL JukkaL added priority-1-normal false-positive mypy gave an error on correct code labels Jul 31, 2018
@PetterS
Copy link
Author

PetterS commented Sep 1, 2018

I wonder how easy this would be to fix? I would be happy if I could fix it, but I haven't had the time to study the mypy code yet.

@gvanrossum
Copy link
Member

The binder is a piece of work, so this may not be an easy project for a beginner. Sorry.

@PetterS
Copy link
Author

PetterS commented Apr 21, 2024

This bug still happens in 1.7.1.

@hauntsaninja
Copy link
Collaborator

This passes after #18138 from @sterliakov :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal
Projects
None yet
Development

No branches or pull requests

4 participants