[pyupgrade] Fix false positive with TypeVar default before Python 3.13 (UP040) - #26888
Merged
Merged
Conversation
… 3.13 (`UP040`) Summary -- This PR fixes a latent bug in `UP040` related to the similar changes to the other PEP-695 rules `UP046` and `UP047` in #21045. In short, `typing_extensions` backports the `default` argument for `TypeVar`s to earlier versions of Python, so checking preview alone isn't sufficient. Even the existing preview check only applied to the `TypeAlias` part of `UP040`, not the `TypeAliasType` part, so I moved the check into `create_diagnostic`, which is shared by both paths. This was never reported, so I'm planning to stabilize the preview feature in 0.16 anyway. Test Plan -- New mdtests and updates to existing snapshots
ntBre
commented
Jul 16, 2026
Contributor
Author
There was a problem hiding this comment.
Preview is disabled here. This demonstrates the partial preview-gate I mentioned in the summary.
|
ntBre
marked this pull request as ready for review
July 16, 2026 16:26
MichaReiser
approved these changes
Jul 17, 2026
| ) { | ||
| // If any type variables have defaults, skip the rule unless | ||
| // running with preview mode enabled and targeting Python 3.13+. | ||
| if type_vars.iter().any(|type_var| type_var.default.is_some()) |
Member
There was a problem hiding this comment.
Nit: Maybe move the cheaper target_version and whether the rule is enabled first checks before the type vars iter check
ntBre
enabled auto-merge (squash)
July 17, 2026 13:04
ntBre
added a commit
that referenced
this pull request
Jul 17, 2026
ntBre
added a commit
that referenced
this pull request
Jul 20, 2026
ntBre
added a commit
that referenced
this pull request
Jul 20, 2026
1 task
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
This PR fixes a latent bug in
UP040related to the similar changes to the other PEP-695 rulesUP046andUP047in #21045. In short,typing_extensionsbackports thedefaultargument forTypeVars to earlier versions of Python, so checking preview alone isn't sufficient. Even theexisting preview check only applied to the
TypeAliaspart ofUP040, not theTypeAliasTypepart, so I moved the check into
create_diagnostic, which is shared by both paths.This was never reported, so I'm planning to stabilize the preview feature in 0.16 anyway.
Test Plan
New mdtests and updates to existing snapshots