Skip to content

Type statement inside class scope not able to reference class generics #19373

Open
@ruipin

Description

@ruipin

Bug Report

I am unable to use type statements inside a class scope that reference that class' generic types. For example, when trying to declare aliases to generic types.

From my reading of PEP 695, this is allowed usage. It works with pyright but not with mypy.

To Reproduce

class A[T]:
    type T2 = T
    type OptionalT = T | None
    type AT = A[T]
    type DictT = dict[str, T]
    type ListT = list[T]

Playground link

Expected Behavior

No errors.

Actual Behavior

main.py:2: error: All type parameters should be declared ("T" not declared)  [valid-type]
main.py:3: error: All type parameters should be declared ("T" not declared)  [valid-type]
main.py:4: error: All type parameters should be declared ("T" not declared)  [valid-type]
main.py:5: error: All type parameters should be declared ("T" not declared)  [valid-type]
main.py:6: error: All type parameters should be declared ("T" not declared)  [valid-type]
Found 5 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.16.1
  • Python version used: 3.13.5
  • No parameters, default config file (see above playground link)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions