Skip to content

test(journeys): a walrus binds to its nearest scope, not to the whole subtree - #432

Merged
Yambr merged 1 commit into
docs/demo-walkthroughfrom
fix/walrus-collector-stops-at-nested-scopes
Aug 11, 2026
Merged

test(journeys): a walrus binds to its nearest scope, not to the whole subtree#432
Yambr merged 1 commit into
docs/demo-walkthroughfrom
fix/walrus-collector-stops-at-nested-scopes

Conversation

@Yambr

@Yambr Yambr commented Aug 11, 2026

Copy link
Copy Markdown
Collaborator

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:

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. At runtime cmd is still ["docker", "ps"].

The fix

The collector 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.

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

  • walrus inside a lambda: clean; walrus in the comprehension: caught; genexp form: caught; plain if (cmd := ...): caught
  • harness 32 passed / 1 skipped, semgrep tree 0
  • mutation: restoring the subtree walk reds 14 cases

One 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.

… 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>
@coderabbitai

coderabbitai Bot commented Aug 11, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

🗂️ Base branches to auto review (2)
  • main
  • next/v1

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 7167180c-f19a-4712-8037-f1888545d514

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Yambr
Yambr merged commit 1024864 into docs/demo-walkthrough Aug 11, 2026
11 checks passed
@Yambr
Yambr deleted the fix/walrus-collector-stops-at-nested-scopes branch August 11, 2026 09:37
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.

2 participants