[ty] Separate typevar shadowing checks for class definitions#23222
Merged
[ty] Separate typevar shadowing checks for class definitions#23222
Conversation
…base The `invalid-generic-class` check was incorrectly flagging nested generic classes whose base class happened to have a type parameter with the same name as an enclosing scope's type parameter. The root cause had two parts: 1. `typevars_referenced_in_definition` collected typevars from both the class's own generic context and its base classes. When walking a base class like `TypedFieldValue[U]`, `walk_specialization` would visit the formal type parameters of `TypedFieldValue`'s generic context (its `T`), even though those are internal to the base class. 2. The check compared collected typevars against enclosing scopes by name only, so `TypedFieldValue`'s `T` would match `CodecBase`'s `T` despite being completely unrelated type variables. The fix splits the check into two parts: - The class's own type parameters are checked by name against enclosing scopes (to catch shadowing like `class C[T]: class Inner[T]: ...`). - Base class typevars are checked by identity against enclosing scopes (to catch actual references like `class C[T]: class Inner(list[T]): ...` while ignoring unrelated same-named typevars from base class definitions). Fixes astral-sh/ty#2780 https://claude.ai/code/session_01H3y6nbL2pZMboFrD5Mqco4
Typing conformance resultsNo changes detected ✅ |
|
Memory usage reportMemory usage unchanged ✅ |
Strip __init_subclass__, class attribute assignment, and extra classes, keeping only the core pattern: a nested generic class inheriting from a base whose formal typevar shares a name with the enclosing scope's typevar. https://claude.ai/code/session_01H3y6nbL2pZMboFrD5Mqco4
sharkdp
approved these changes
Feb 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes astral-sh/ty#2780
This PR refactors the typevar shadowing validation for generic classes to better distinguish between two separate concerns:
A class's own type parameters should not shadow type variables from enclosing scopes by name. This check uses
generic_context.variables()andbinds_named_typevar().A class's base classes should not reference type variables bound in enclosing scopes. This check uses
typevars_referenced_in_bases()andbinds_typevar().Previously, both checks were conflated in a single method
typevars_referenced_in_definition(). The separation avoids a confusing false positive in the added test case. A nested generic class can safely inherit from a generic base that happens to have a type parameter with the same name as the enclosing scope's type parameter.Directly referencing an enclosing scope's type variable in the base class list remains an error. (Pyright actually relaxes this rule for PEP 695 typevars, but mypy does not -- it seems OK to stay stricter for now, to reduce the changes in this PR; we can relax this later if it comes up.)
Test Plan
Added test case in
scoping.mddemonstrating the valid pattern of a nested generic class inheriting from a generic base with same-named type parameters, along with the still-invalid pattern of directly referencing enclosing scope type variables in base classes. There are already tests for a nested generic shadowing an outer typevar with its own new typevar.