python / python/mypy

Mypy not reporting "undefined" variable.

Open
#15,416 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the behavior with the main.py example and mypy --strict main.py. Review how the checker handles an annotation-only variable before deciding whether this case should receive a diagnostic like [var-undefined]. Done means the behavior is either covered by an appropriate test and diagnostic, or the expected behavior is documented as unsupported.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.