mypy reports no issues on access of conditionally available fields
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
To Reproduce
# foo.py
def print_foo():
print(foo)
if __name__ == '__main__':
foo = "foobar"
https://mypy-play.net/?mypy=latest&python=3.12&gist=d8a24e474f61a151f549728e14173383
Expected Behavior
mypy should report an error
the name foo is not guaranteed to be part of the scope in print_foo. E.g. the following code will break
import foo
foo.print_foo()
Actual Behavior
mypy outputs "Success: no issues found in 1 source file"
Your Environment
see playground link
- Mypy version used: 1.15
- Mypy command-line flags: none (but happens with --strict also)
- Mypy configuration options from
mypy.ini(and other config files): - Python version used: 3.11, 3.12, 3.13
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the minimal foo.py reproduction and run it through the linked mypy playground across the reported Python versions. Trace how mypy handles the conditionally defined global accessed by print_foo; done means it reports an error for the example while preserving valid scope analysis.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers, devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100