[pyupgrade] Skip fix when a defaulted TypeVar precedes a non-defaulted one (UP040, UP046, UP047) - #27133
Merged
ntBre merged 3 commits intoJul 27, 2026
Conversation
…ulted one (`UP040`, `UP046`, `UP047`) In a PEP 695 type parameter list, a type parameter without a default cannot follow one with a default, and reordering the parameters would change the meaning of subscriptions, so there is no valid, equivalent fix. Bail out of the diagnostic instead of emitting invalid syntax. Fixes astral-sh#27021
|
ntBre
reviewed
Jul 24, 2026
Contributor
There was a problem hiding this comment.
Thanks! The code changes here look good to me. I just had a few minor suggestions to trim down the comments a bit, and a request to use our new mdtest format.
Don't worry too much about the ecosystem check yet, that's just from an old merge base and should clear up if you push another commit.
- Replace the new UP040/UP046/UP047 fixture cases and snapshot updates with an mdtest at resources/mdtest/pyupgrade/pep-695.md covering all three PEP 695 rules - Drop the duplicated call-site comments; the docstring on non_default_follows_default explains the check - Trim the docstring paragraph about fix equivalence
Contributor
Author
|
Thanks @ntBre for the suggestions. Addressed all of them. |
ntBre
approved these changes
Jul 27, 2026
ntBre
left a comment
Contributor
There was a problem hiding this comment.
Looks good to me, thank you!
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 #27021.
In a PEP 695 type-parameter list, a non-defaulted parameter cannot follow a defaulted one, but the UP040/UP046/UP047 fixes generated exactly that when the source declared e.g.
T = TypeVar("T", default=int)beforeS = TypeVar("S")— producingtype Pair[T = int, S] = tuple[T, S], which ruff itself then rejects as invalid syntax.Reordering the parameters would not be an equivalent fix: parameter order determines how positional arguments bind when subscripting the alias/class/function (
Pair[int, str]). So, per the issue's suggestion and following the existing precedent in these rules (duplicate TypeVars and defaults on unsupported targets also suppress the diagnostic;NonPEP695TypeAliasdeclaresFixAvailability::Always, so a fixless diagnostic isn't an option), the diagnostic is skipped when a non-defaulted TypeVar follows a defaulted one.The guard lives at both choke points (
check_type_varsfor UP046/UP047 andcreate_diagnosticfor both UP040 paths, includingTypeAliasType(..., type_params=...)), so all three rules are covered by one helper.Test Plan
UP040.py,UP046_0.py,UP047_0.py: defaulted-then-non-defaulted (no diagnostic), non-defaulted-first and all-defaulted (still fixed correctly); snapshots regenerated.cargo test -p ruff_linter— all pass;cargo fmt --checkandcargo clippy -p ruff_linter --no-deps -- -D warningsclean.Developed with AI assistance (Claude Code); I reviewed the change and can speak to it.