Repository navigation
[refurb] Parenthesize yield arguments in the FURB192 fixer - #27192
Merged
Merged
Conversation
`sorted((yield))[0]` was rewritten to `min(yield)`, which is invalid syntax, so the fix was rejected with "Fix introduced a syntax error". Slice the parenthesized range of the argument instead of the bare node, the same way `quadratic-list-summation` does.
ntBre
approved these changes
Jul 27, 2026
ntBre
left a comment
Contributor
There was a problem hiding this comment.
Thanks, this looks good to me.
One deliberate side effect worth flagging: parentheses are now kept for any parenthesized argument, so sorted((a := b))[0] becomes min((a := b)) rather than min(a := b). Both are valid; keeping them seemed more consistent than special-casing yield. Happy to narrow it to just Expr::Yield/Expr::YieldFrom if you'd rather not change the other cases.
I think this does make sense and matches how we use parenthesized_range elsewhere.
|
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
The
FURB192fix rebuilds the call by slicing the source text of thesorted()argument node. That range doesn't include surrounding parentheses, so a parenthesizedyieldloses them and the result is invalid syntax:The replacement it tries to write is
x = min(yield). Ayieldexpression is only valid as a call argument when it's parenthesized, so the fix gets discarded and the diagnostic is left unfixable.All four forms are affected on
main(a5cdc6d):sorted((yield))[0]min(yield)sorted((yield x))[-1]max(yield x)sorted((yield), key=k)[0]min(yield, key=k)sorted((yield from g()))[0]min(yield from g())This slices
parenthesized_range()for the argument and falls back to the node range, which is whatquadratic-list-summationand a few other rules already do. #24200 fixed the same kind of thing in theFURB142fixer.One deliberate side effect worth flagging: parentheses are now kept for any parenthesized argument, so
sorted((a := b))[0]becomesmin((a := b))rather thanmin(a := b). Both are valid; keeping them seemed more consistent than special-casingyield. Happy to narrow it to justExpr::Yield/Expr::YieldFromif you'd rather not change the other cases.Test Plan
Added
FURB192_1.py. It has to be a separate fixture:FURB192.pyshadowssortedwith a module-leveldef sorted()at the bottom, and because function bodies are resolved against the final module scope, the rule doesn't fire inside any function in that file — soyieldcases can't go there.cargo test -p ruff_linter— 2807 passed, 0 failedcargo clippy -p ruff_linter --all-targets --all-features -- -D warnings— cleanprek run --files <changed files>— cleanI also ran
--select FURB192 --fix --unsafe-fixesover all 1599 files undercrates/ruff_linter/resources/test/fixtures, once with 0.16.0 and once with this branch, and diffed the output. The only file whose result changes is the new fixture, and neither build produces unparseable output anywhere else.