Skip to content

mypy thinks variable is None even if assigned non-None value inside loop #11900

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
Akuli opened this issue Jan 4, 2022 · 3 comments
Closed
Labels
bug mypy got something wrong

Comments

@Akuli
Copy link
Contributor

Akuli commented Jan 4, 2022

Bug Report

To Reproduce

def foo() -> None:
    user = None

    while True:
        if user is None:
            user = 123
        else:
            print(1 + "lol")

foo()

Expected Behavior

mypy error + runtime error

Actual Behavior

no mypy error

Your Environment

  • Mypy version used: 0.930
@Akuli Akuli added the bug mypy got something wrong label Jan 4, 2022
@Akuli
Copy link
Contributor Author

Akuli commented Jan 4, 2022

Workaround: user: int | None = None or user: Optional[int] = None

@zhangck981
Copy link

I believe the problem might be something around the keywordis. Here are my test cases:
The first one is the case you provided:
Screen Shot 2022-04-02 at 16 43 26
And the second test case is:
Screen Shot 2022-04-02 at 16 44 06

I changed the keyword is to the operator == and it shows that mypy detects the error. Therefore, I believe the problem can be located with the keyword is.
I will continue working on this by further inspecting the source code and updating my process! However, I am new to mypy and not familiar with that. It would be best if you can provide me with some information that which files I should pay more attention to.

@A5rocks
Copy link
Collaborator

A5rocks commented Jan 22, 2025

Fixed by #18433.

@A5rocks A5rocks closed this as completed Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

3 participants