test(journeys): a walrus binds to its nearest scope, not to the whole subtree - #432
Merged
Yambr merged 1 commit intoAug 11, 2026
Conversation
… subtree
The walrus collector swept the entire comprehension with `ast.walk`, so one
written inside a NESTED lambda was attributed to the enclosing function and
reddened that function's clean list argv:
def f(x):
cmd = ["docker", "ps"]
fns = [lambda: (cmd := f"rm {x}") for _ in range(1)]
subprocess.run(cmd) # flagged, and clean
PEP 572 binds a walrus in the scope that contains it — inside a lambda that is
the lambda, not the function. The collector now recurses through direct
children and stops at every nested scope, the same pruning the nested-def fix
applied. A walrus written in the comprehension itself still binds outward and
is still caught, in both list and generator forms.
Latent rather than live: the harness contains no walrus today, so the tree was
0-hit either way. It is the same reds-on-the-safe-form failure the earlier
scoping work drove out.
Mutation-checked: restoring the subtree walk reds 14 cases. The first probe of
this was invalid — it failed to parse, so its green measured a build error
rather than the guard.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. 🗂️ Base branches to auto review (2)
Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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.
Follow-up to #430, from a review pass on the merged guard.
The defect
The walrus collector swept the entire comprehension with
ast.walk, so a:=written inside a nested lambda was attributed to the enclosing function and reddened that function's clean list argv:PEP 572 binds a walrus in the scope that contains it — inside a lambda that is the lambda, not the function. At runtime
cmdis still["docker", "ps"].The fix
The collector recurses through direct children and stops at every nested scope, the same pruning the nested-
deffix applied. A walrus written in the comprehension itself still binds outward and is still caught, in both list and generator forms.Status
Latent, not live. The harness contains no walrus expression today, so the tree was 0-hit either way. It is worth closing because it is the same reds-on-the-safe-form failure the earlier scoping work drove out, and a guard that reds on the safe form forces the per-site waivers off instead of keeping them honest.
Measured
if (cmd := ...): caughtOne note on method: my first mutation probe of this failed to parse, so its green measured a build error rather than the guard. Redone with the indentation preserved before the result was read.