fix(linter): suppress FURB122 fix when walrus operator rebinds loop variable#24566
Open
hijingsong wants to merge 1 commit intoastral-sh:mainfrom
Open
fix(linter): suppress FURB122 fix when walrus operator rebinds loop variable#24566hijingsong wants to merge 1 commit intoastral-sh:mainfrom
hijingsong wants to merge 1 commit intoastral-sh:mainfrom
Conversation
…ariable
When the argument to `.write()` in a for-loop contains a walrus operator
(:=) whose target is one of the for-loop's iteration variables, the
FURB122 fix would generate an invalid comprehension. For example:
for line in src:
dst.write(line := line.upper())
would be transformed to:
dst.writelines(line := line.upper() for line in src)
which is a SyntaxError because the walrus operator cannot rebind a
comprehension iteration variable.
Suppress the fix in such cases by detecting walrus operators in the
write argument whose targets match any for-loop binding names.
Closes: astral-sh#21107
MichaReiser
reviewed
Apr 13, 2026
| found: bool, | ||
| } | ||
|
|
||
| impl<'a> Visitor<'a> for WalrusChecker<'a> { |
Member
There was a problem hiding this comment.
You can use any_over_expr over writing your own visitor. I also suggest you take a look at the previous PRs trying to fix this issue. Maybe there's some test cases or edge cases that are worth copying over:)
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
Suppresses the FURB122 (
for-loop-writes) fix when the.write()argument contains a walrus operator (:=) whose target is one of the for-loop's iteration variables.The fix would otherwise generate an invalid comprehension. For example:
would be transformed to:
which produces
SyntaxError: assignment expression cannot rebind comprehension iteration variable 'line'.Similarly for tuple unpacking:
Test Plan
Added test cases to
FURB122.pyfixture:dst.write(line := line.upper())wherelineis the loop variabledst.write(rest := "".join(rest))whererestis a loop variable from tuple unpackingf.write(y := process(x))whereyis NOT a loop variable — the fix is still valid hereCloses: #21107