[ty] Reject functions with PEP-695 type parameters that shadow type parameters from enclosing scopes#23619
Merged
AlexWaygood merged 5 commits intomainfrom Feb 27, 2026
Merged
Conversation
…ng scopes Generic functions and methods with PEP 695 type parameters that shadow type variables from enclosing generic scopes are now flagged as errors, matching the existing check for generic classes. https://claude.ai/code/session_01Eojz9eLtLnKtAF6LcxZczF
Replace uses of `invalid-generic-class` for the rebound-typevar case with a dedicated `shadowed-type-variable` error code, used consistently for both class and function definitions that shadow type variables from enclosing scopes. https://claude.ai/code/session_01Eojz9eLtLnKtAF6LcxZczF
Drop "that is" from the message for conciseness. https://claude.ai/code/session_01Eojz9eLtLnKtAF6LcxZczF
Typing conformance results improved 🎉The percentage of diagnostics emitted that were expected errors increased from 85.40% to 85.45%. The percentage of expected errors that received a diagnostic increased from 75.55% to 75.82%. Summary
True positives addedDetails
|
|
Memory usage reportMemory usage unchanged ✅ |
Merge `report_rebound_typevar` and `report_rebound_typevar_function`
into a single `report_shadowed_type_variable` that takes the entity
kind ("class"/"function"), name, and range as parameters.
https://claude.ai/code/session_01Eojz9eLtLnKtAF6LcxZczF
309c517 to
1bb9898
Compare
|
| Lint rule | Added | Removed | Changed |
|---|---|---|---|
invalid-generic-class |
0 | 5 | 0 |
shadowed-type-variable |
5 | 0 | 0 |
invalid-argument-type |
0 | 1 | 0 |
| Total | 5 | 6 | 0 |
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
We currently reject both of these classes due to the fact that their type parameters shadow type parameters bound by enclosing scopes:
but we do not currently reject either of these functions, which are equally invalid for the same reason:
This PR adds the same validation for functions that we have for classes. The validation for generic classes was emitted using the
invalid-generic-classerror code, however, which isn't appropriate for generic functions. It would be confusing for the same check to be split between two error codes, so this PR adds a newshadowed-type-variablediagnostic. The new diagnostic is used for the new validation, and our old validation is updated to also use the new diagnostic.Test Plan
mdtests and snapshots