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

Mypy not reporting "undefined" variable. #15416

Open
neimad1985 opened this issue Jun 11, 2023 · 1 comment
Open

Mypy not reporting "undefined" variable. #15416

neimad1985 opened this issue Jun 11, 2023 · 1 comment
Labels
bug mypy got something wrong

Comments

@neimad1985
Copy link

Bug Report

When using a "undefined" variable, which will immediately crash the code when running it, mypy does not report anything about it. Is it normal ?

I feel like mypy should report it but maybe I am wrong because this isn't a typing error. On the other hand, this error is only possible because typing annotations exist . What do you think ?

To Reproduce

Dump this in a file main.py:

number: int
print(number)

Gist URL: https://gist.github.com/mypy-play/31d3775229a6fe01af6bbb09b9230a18
Playground URL: https://mypy-play.net/?mypy=latest&python=3.11&gist=31d3775229a6fe01af6bbb09b9230a18

Expected Behavior

An error message, maybe something like this.

main.py:2: error: Using an undefined variable, a value must be set before first.  [var-undefined]

Actual Behavior

$ mypy --strict main.py
Success: no issues found in 1 source file
python main.py
Traceback (most recent call last):
  File "/home/user/main.py", line 2, in <module>
    print(number)
          ^^^^^^
NameError: name 'number' is not defined

Your Environment

  • Mypy version used: 1.3.0
  • Mypy command-line flags: --strict
  • Mypy configuration options from mypy.ini (and other config files): nothing
  • Python version used: 3.11.3
@neimad1985 neimad1985 added the bug mypy got something wrong label Jun 11, 2023
@ikonst
Copy link
Contributor

ikonst commented Jun 11, 2023

From mypy's perspective, "uninitialized" or "unassigned" probably described it better than "undefined". Maybe change the title to "Not reporting uninitialized variable read"?

This seems related to #4019 but not quite the same (at first glance, #4019 seems much more complicated, since a declared class attribute might or might not be initialized elsewhere).

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

2 participants