[refurb] Suppress FURB122 fix when walrus operator rebinds a loop variable#26382
Draft
kpeis695 wants to merge 1 commit into
Draft
[refurb] Suppress FURB122 fix when walrus operator rebinds a loop variable#26382kpeis695 wants to merge 1 commit into
FURB122 fix when walrus operator rebinds a loop variable#26382kpeis695 wants to merge 1 commit into
Conversation
When `write_arg` contains a named expression (walrus operator) whose target is one of the `for`-loop iteration variables, converting the loop to a generator expression produces a SyntaxError: PEP 572 forbids an assignment expression from rebinding a comprehension iteration variable. Skip the diagnostic entirely for those cases to avoid emitting a fix that would break the code. Fixes astral-sh#21107 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Author
|
Same issue as the PYI036 PR. Branched incorrectly and picked up unrelated commits. Reopened from a clean branch with only the FURB122 fix. |
Contributor
|
Thanks for reopening these, but I'm going to convert them back to drafts for now. For a first-time contribution, let's focus on getting one PR to a resolution before opening more for review. Let's start with #26354 since I've already started reviewing it. |
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 #21107.
When
write_argcontains a named expression (walrus operator:=) whose target is one of thefor-loop iteration variables, the FURB122 fix converts:to:
which is a
SyntaxError— PEP 572 forbids an assignment expression from rebinding a comprehension iteration variable.This PR adds a
write_arg_rebinds_loop_varhelper that walkswrite_argwithany_over_expr, looking for anExprNamedwhose target name matches any loop iteration variable. When found, the diagnostic is skipped entirely to avoid emitting a broken fix.The same guard covers tuple-destructured loop targets (e.g.
for first, *rest in src: dst.write(rest := ...)).Walrus operators that bind a different name (not a loop variable) still produce the diagnostic and fix, as before.
Test Plan
Added four test cases to
FURB122.py:other(not a loop var) — fix still applies.cargo test -p ruff_linter -- refurbpasses (44/44).