Skip to content
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

Reference before assignment errors #13976

Closed
jrab89 opened this issue Oct 31, 2022 · 3 comments
Closed

Reference before assignment errors #13976

jrab89 opened this issue Oct 31, 2022 · 3 comments
Labels
bug mypy got something wrong

Comments

@jrab89
Copy link

jrab89 commented Oct 31, 2022

Bug Report

mypy doesn't catch "reference before assignment" errors

To Reproduce

import os
from typing import Optional


def greet(name: Optional[str]) -> None:
    if name:
        name_str = f", {name}."

    print(f"Hello{name_str}")
    name_str = "bar"


def main() -> None:
    greet(os.environ.get("NAME"))


if __name__ == "__main__":
    main()

Expected Behavior

If the above program is ran without NAME set, it'll crash:

$ NAME='Jeff' python reference_before_assign.py
Hello, Jeff.
$ python reference_before_assign.py
Traceback (most recent call last):
  File "/Users/jeff.rabovsky/git_repos/notes/reference_before_assign.py", line 17, in <module>
    main()
  File "/Users/jeff.rabovsky/git_repos/notes/reference_before_assign.py", line 13, in main
    greet(os.environ.get("NAME"))
  File "/Users/jeff.rabovsky/git_repos/notes/reference_before_assign.py", line 9, in greet
    print(f"Hello{name_str}")
UnboundLocalError: local variable 'name_str' referenced before assignment

My expectation was that mypy would catch this error.

Actual Behavior

$ mypy --strict reference_before_assign.py
Success: no issues found in 1 source file

Your Environment

  • Mypy version used: 0.982
  • Mypy command-line flags: --strict
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.10.5

Thanks for your help!

@jrab89 jrab89 added the bug mypy got something wrong label Oct 31, 2022
@hauntsaninja
Copy link
Collaborator

Thanks, duplicate of #2400 , #686 , etc. Is actively being worked on these days

@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Oct 31, 2022
@AlexWaygood AlexWaygood reopened this Oct 31, 2022
@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale Oct 31, 2022
@AlexWaygood
Copy link
Member

(Sorry, reopened by accident...)

@PeterJCLaw
Copy link
Contributor

Connecting the dots as the linked issues are closed: looks like #4019 tracks this case now.

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

4 participants