python / python/mypy

mypy reports no issues on access of conditionally available fields

Open
#18,708 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-possibly-undefined
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

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.