Incorrect type parameter scope inference with nested classes
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
In a class whose body is nested in another, mypy doesn't recognize the type parameters of the outer class as bound in the inner.
To Reproduce
# test_mypy.py
class Foo[T]:
class Bar[R]:
def foobar(self, t: T, r: R, /) -> None: ...
print(T, R)
Output of program
T R
Note that there is no NameError being thrown. The names T and R are indeed both bound in the scope of the definition of R.
Actual Behavior
test_mypy.py:3: error: Name "T" is not defined [name-defined]
Found 1 error in 1 file (checked 1 source file)
Your Environment
- Mypy version used:
- Command used: mypy test_mypy.py
- Mypy configuration options from
mypy.ini(and other config files): None relevant - Python version used: 3.12, 3.13, 3.14
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 mypy test_mypy.py with the nested Foo[T] and Bar[R] example from the report, then trace the type-parameter scope handling for nested class bodies. Add a regression test showing that the outer T is recognized alongside inner R, and confirm the name-defined error no longer appears.
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
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100