Skip to content

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

Closed

Description

@Akuli

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

Activity

  1. added
    bugmypy got something wrong
    on Jan 4, 2022
  2. Akuli commented on Jan 4, 2022

    @Akuli
    ContributorAuthor

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

  3. zhangck981 commented on Apr 2, 2022

    @zhangck981

    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.

  4. 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