Skip to content

Incorrect type parameter scope inference with nested classes #21210

@jonathandung

Description

@jonathandung

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-pep-695Issues related to PEP 695 syntax

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions