Skip to content

[pyupgrade] Fix false positive with TypeVar default before Python 3.13 (UP040) - #26888

Merged
ntBre merged 4 commits into
mainfrom
brent/up040-defaults
Jul 17, 2026
Merged

ntBre merged 4 commits into
mainfrom
brent/up040-defaults

Conversation

@ntBre

@ntBre ntBre commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

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
TypeVars 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 added 3 commits July 16, 2026 12:17
… 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 ntBre added bug Something isn't working rule Implementing or modifying a lint rule preview Related to preview mode features labels Jul 16, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Preview is disabled here. This demonstrates the partial preview-gate I mentioned in the summary.

@astral-sh-bot

astral-sh-bot Bot commented Jul 16, 2026

Copy link
Copy Markdown

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@ntBre
ntBre marked this pull request as ready for review July 16, 2026 16:26
@ntBre
ntBre requested a review from MichaReiser July 16, 2026 16:26
) {
// 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())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Maybe move the cheaper target_version and whether the rule is enabled first checks before the type vars iter check

@ntBre
ntBre enabled auto-merge (squash) July 17, 2026 13:04
@ntBre
ntBre merged commit 7bdf9a8 into main Jul 17, 2026
46 checks passed
@ntBre
ntBre deleted the brent/up040-defaults branch July 17, 2026 13:08
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working preview Related to preview mode features rule Implementing or modifying a lint rule

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants