Skip to content

Conversation

@fangyi-zhou
Copy link
Contributor

Summary

Per spec for constructor calls https://typing.python.org/en/latest/spec/constructors.html#init-method

Class-scoped type variables should not be used in the self annotation
because such use can lead to ambiguous or nonsensical type evaluation
results.

This diff implements this requirement by adding a specific and targeted check. If a type variable is used in the self annotation in the __init__ method, an error will be raised if the type variable is defined in the class scope.

This gives us another file with passing conformance tests.

Test Plan

Added new test case, checking for type parameters declared via both Generic[T] and syntax.

python test.py

Per spec for constructor calls https://typing.python.org/en/latest/spec/constructors.html#init-method

> Class-scoped type variables should not be used in the self annotation
> because such use can lead to ambiguous or nonsensical type evaluation
> results.

This diff implements this requirement by adding a specific and targeted
check. If a type variable is used in the `self` annotation in the
`__init__` method, an error will be raised if the type variable is
defined in the class scope.
@meta-cla meta-cla bot added the cla signed label Jan 28, 2026
@github-actions
Copy link

Diff from mypy_primer, showing the effect of this PR on open source code:

mypy (https://github.com/python/mypy)
+ ERROR mypy/typeshed/stdlib/unittest/mock.pyi:248:13-21: `__init__` method self type cannot reference class type parameter `_T` [invalid-annotation]
+ ::error file=mypy/typeshed/stdlib/unittest/mock.pyi,line=248,col=13,endLine=248,endColumn=21,title=Pyrefly invalid-annotation::`__init__` method self type cannot reference class type parameter `_T`

@meta-codesync
Copy link

meta-codesync bot commented Jan 28, 2026

@migeed-z has imported this pull request. If you are a Meta employee, you can view this in D91648059.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant