Name binding can be wrong when unrelated forward references are involved
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
There is a very weird issue with classes in functions that are bound to names outside of the function, but only if forward references are involved. It is not a reveal_type issue and can occur with normal inheritance like class Foo(A) as well, which is how it was discovered.
To Reproduce
class A: pass
class B: pass
def foo() -> None:
class Unrelated(list['ForwardReference']): ...
class ForwardReference(): pass
class A():
def x(self) -> None: ...
def bar() -> None:
reveal_type(A) # Should be E: Revealed type is "def () -> errorx2.A"
reveal_type(B)
A().x() # Should be an AttributeError
Will result in:
main.py:11: note: Revealed type is "def () -> __main__.A@7"
main.py:12: note: Revealed type is "def () -> __main__.B"
Success: no issues found in 1 source file
See also https://mypy-play.net/?mypy=latest&python=3.11&gist=7238759e8e27220c3b67ac9d1114d45b
Expected Behavior
The reveal type should be def () -> __main__.A instead of def () -> __main__.A@7.
Your Environment
- Mypy version used: Current latest (see Playground)
- Mypy command-line flags: None
- Mypy configuration options from
mypy.ini(and other config files): None - Python version used: 3.11
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 by running the provided Python reproducer with mypy and compare the revealed type for A with the expected output. Trace name binding for local classes, unrelated forward references, and inheritance; done means the revealed type refers to the module-level A and the example reports the expected invalid attribute access.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100