[ty] Validate type variable defaults don't reference later type parameters or type parameters out of scope#23623
Merged
AlexWaygood merged 13 commits intomainfrom Feb 28, 2026
Merged
Conversation
…variables Type variable defaults can reference earlier type variables in the parameter list (PEP 696), but not later ones. For example, `class C[T, U = T]` is valid, but `class C[S = T, T = int]` is not because S's default references T which is defined after S. This adds a check in class body inference that walks PEP 695 type parameter defaults and reports `invalid-generic-class` if any default references a type variable that appears later in the parameter list. https://claude.ai/code/session_01CmptNs5MMq5fsoQr7Ahqai
The verbose message is a general statement about the rule; the concise message (used with --output-format=concise) names the specific type parameters involved. https://claude.ai/code/session_01CmptNs5MMq5fsoQr7Ahqai
Drop the separate concise message; the single message is short enough to work in both modes. https://claude.ai/code/session_01CmptNs5MMq5fsoQr7Ahqai
The existing check that type variable defaults don't reference later type
parameters only applied to PEP 695 generic contexts. This extends it to
legacy `Generic[...]` contexts as well, so that e.g.
`class Foo(Generic[T2, T1])` where `T2 = TypeVar("T2", default=T1)`
correctly emits an error.
https://claude.ai/code/session_01CmptNs5MMq5fsoQr7Ahqai
Typing conformance results improved 🎉The percentage of diagnostics emitted that were expected errors increased from 85.39% to 85.42%. The percentage of expected errors that received a diagnostic increased from 76.01% to 76.19%. Summary
True positives addedDetails
|
|
Memory usage reportMemory usage unchanged ✅ |
|
| Lint rule | Added | Removed | Changed |
|---|---|---|---|
invalid-await |
0 | 40 | 0 |
invalid-return-type |
0 | 1 | 0 |
type-assertion-failure |
1 | 0 | 0 |
| Total | 1 | 41 | 0 |
…type parameter list Previously, the check for invalid typevar default references only looked for typevars appearing *later* in the same type parameter list. This missed the case where a typevar's default references a typevar that isn't in the list at all. For example, `class Bad(Generic[Start2T, Stop2T, StepT])` where `Start2T`'s default is `StopT` (a different typevar not in the class's parameter list) was not flagged. The fix replaces `contains_typevar` (checking specific later typevars) with `find_typevar_not_in` (finding any typevar not in the set of earlier typevars). This catches both later-in-list and not-in-list cases. https://claude.ai/code/session_01CmptNs5MMq5fsoQr7Ahqai
… out of scope When the referenced type variable is totally out of scope (not in the class's type parameter list at all), we only show the secondary annotation for the typevar with the bad default, not the out-of-scope one. https://claude.ai/code/session_01CmptNs5MMq5fsoQr7Ahqai
charliermarsh
approved these changes
Feb 28, 2026
AlexWaygood
pushed a commit
that referenced
this pull request
Feb 28, 2026
PR #23623 added class-level validation for type variable defaults referencing out-of-scope type parameters. Remove the overlapping class-specific code and tests from this branch, keeping only the function and type alias scope checks. https://claude.ai/code/session_018eEiLxueUR7nji1dChCRrm
AlexWaygood
pushed a commit
that referenced
this pull request
Feb 28, 2026
PR #23623 added class-level validation for type variable defaults referencing out-of-scope type parameters. Remove the overlapping class-specific code and tests from this branch, keeping only the function and type alias scope checks. https://claude.ai/code/session_018eEiLxueUR7nji1dChCRrm
carljm
added a commit
that referenced
this pull request
Mar 2, 2026
* main: [ty] Move binary expression logic out of `builder.rs` (#23649) [ty] Limit recursion depth when displaying self-referential function types (#23647) [ty] Move comparison logic out of `builder.rs` (#23646) Avoid inserting redundant `None` elements in UP045 (#23459) [ty] Add more ParamSpec validation for `P.args` and `P.kwargs` (#23640) [ty] Sync vendored typeshed stubs (#23642) [ty] Fix inference of `t.__mro__` if `t` is an instance of `type[Any]` (#23632) [ty] Detect inconsistent generic base class specializations (#23615) [ty] Validate type variable defaults don't reference later type parameters or type parameters out of scope (#23623) [ty] Ban nested `Required`/`NotRequired`, and ban them both outside of `TypedDict` fields (#23627) [ty] Reject generic metaclasses parameterized by type variables (#23628) Update default Python version examples (#23605) fix binops with NewType of float and Unknown (#23620) [ty] Reject functions with PEP-695 type parameters that shadow type parameters from enclosing scopes (#23619) [ty] Reject ellipsis literals in odd places in type/annotation expressions (#23611) [ty] hash-cons `UseDefMap` fields (#23283)
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
Add validation to ensure that type variable defaults cannot reference type variables that appear later in the type parameter list, or type parameters that are out of scope. This is a restriction in both PEP 695 and legacy generic syntax.
For example, in
class Bad[S = T, T = int], the default forSreferencesTwhich comes after it, which is invalid. However,class Good[T, U = T]is valid sinceU's default referencesTwhich comes before it.Test Plan
mdtests and snapshots