Skip to content

[refurb] Suppress FURB122 fix when walrus operator rebinds a loop variable#26382

Draft
kpeis695 wants to merge 1 commit into
astral-sh:mainfrom
kpeis695:fix/furb122-clean
Draft

[refurb] Suppress FURB122 fix when walrus operator rebinds a loop variable#26382
kpeis695 wants to merge 1 commit into
astral-sh:mainfrom
kpeis695:fix/furb122-clean

Conversation

@kpeis695

Copy link
Copy Markdown

Summary

Fixes #21107.

When write_arg contains a named expression (walrus operator :=) whose target is one of the for-loop iteration variables, the FURB122 fix converts:

for line in src:
    dst.write(line := line.upper())

to:

dst.writelines(line := line.upper() for line in src)

which is a SyntaxError — PEP 572 forbids an assignment expression from rebinding a comprehension iteration variable.

This PR adds a write_arg_rebinds_loop_var helper that walks write_arg with any_over_expr, looking for an ExprNamed whose 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:

  • Three "OK" cases: simple loop var rebind, tuple loop var rebind, one-of-two loop vars rebind — all suppressed.
  • One "Error" case: walrus binds other (not a loop var) — fix still applies.

cargo test -p ruff_linter -- refurb passes (44/44).

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>
@astral-sh-bot astral-sh-bot Bot requested a review from ntBre June 25, 2026 16:59
@kpeis695

Copy link
Copy Markdown
Author

Same issue as the PYI036 PR. Branched incorrectly and picked up unrelated commits. Reopened from a clean branch with only the FURB122 fix.

@ntBre

ntBre commented Jun 25, 2026

Copy link
Copy Markdown
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.

@ntBre ntBre marked this pull request as draft June 25, 2026 18:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FURB122 fix introduces syntax error for rebound comprehension variable

3 participants