Skip to content

None-ness falsely assumed in loop #11430

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 Nov 1, 2021 · 2 comments
Closed

None-ness falsely assumed in loop #11430

Akuli opened this issue Nov 1, 2021 · 2 comments
Labels
bug mypy got something wrong

Comments

@Akuli
Copy link
Contributor

Akuli commented Nov 1, 2021

Bug Report

After foo = None, mypy thinks that foo will always be None, even if a loop can jump between the assignment and the use of foo.

To Reproduce

def main() -> None:
    gonna_select = None
    while True:
        if gonna_select is not None:
            print(1 + "lol")
        gonna_select = "lol"

Expected Behavior

error from 1 + "lol"

Actual Behavior

no error

Your Environment

  • Mypy version used: 0.910
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.7
@Akuli Akuli added the bug mypy got something wrong label Nov 1, 2021
@Akuli Akuli changed the title "is not None" falsely not analyzed None-ness falsely assumed Nov 1, 2021
@hauntsaninja hauntsaninja changed the title None-ness falsely assumed None-ness falsely assumed in loop Nov 2, 2021
@intgr
Copy link
Contributor

intgr commented Nov 8, 2021

def main() -> None:
    gonna_select = None
    while True:
        gonna_select = "lol"
        # ^^^^^^^^^^^^^^^^^^

I think the real error should be here. Since mypy infers gonna_select as type None, why does it allow assigning a string to it?

@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