Skip to content

None-ness falsely assumed in loop #11430

Closed

Description

@Akuli

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

Activity

  1. added
    bugmypy got something wrong
    on Nov 1, 2021
  2. changed the title [-]"is not None" falsely not analyzed[/-] [+]None-ness falsely assumed[/+] on Nov 1, 2021
  3. changed the title [-]None-ness falsely assumed[/-] [+]None-ness falsely assumed in loop[/+] on Nov 2, 2021
  4. intgr commented on Nov 8, 2021

    @intgr
    Contributor
    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?

  5. A5rocks commented on Jan 22, 2025

    @A5rocks
    Collaborator

    Fixed by #18433

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions