"error: Name is not defined" and confusing behavior on instance-level classes
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Running mypy on the following example:
from typing import Type
class Foo:
def __init__(self):
class Bar:
pass
self.Bar: Type[Bar] = Bar
foo = Foo()
bar: foo.Bar = foo.Bar()
reveal_type(foo.Bar)
reveal_type(bar)
results in the following slightly confusing and seemingly contradictory output:
example.py:11: error: Name 'foo.Bar' is not defined # on bar: foo.Bar
example.py:13: note: Revealed type is 'Type[example.Bar@5]' # on reveal_type(foo.Bar)
example.py:14: note: Revealed type is 'Any' # on reveal_type(bar)
Ideally, I would have expected reveal_type(bar) to reveal example.Bar@5. I'm not sure if there is a way to correctly type this code without resorting to # type: ignore right now.
Tested on mypy 0.761, Python 3.8.1.
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
Reproduce the example in example.py with mypy, focusing on the annotation bar: foo.Bar and the reveal_type calls. Trace how mypy handles the instance-level class reference and determine whether the reported error and Any result can be replaced with the expected example.Bar@5 type. Done means the example type-checks as intended and reveal_type(bar) reports that class type.
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
- 35/100