Skip to content

Give a comprehension's walrus target the containing scope's symbol - #40

Merged
knutwannheden merged 2 commits into
mainfrom
comprehension-walrus-symbol-scope
Dec 4, 2026
Merged

knutwannheden merged 2 commits into
mainfrom
comprehension-walrus-symbol-scope

Conversation

@knutwannheden

Copy link
Copy Markdown
Contributor

Under includeSymbols, a walrus target inside a comprehension or generator expression was described as a local of the comprehension, although PEP 572 binds it in the containing scope. Where the containing scope also assigns the name, one variable got two symbol ids:

def partial(values):
    total = 0                                    # id 2
    sums = [total := total + v for v in values]  # target: id 4, read: id 2
    return total                                 # id 4: m.<locals of function 'partial'>.<locals of list comprehension>.total

With this change all four references share one id, m.<locals of function 'partial'>.total. A module-level walrus in a comprehension reads m.hit rather than m.<locals of list comprehension>.hit. This matches what the README already promises for global and nonlocal names: the id of the scope that owns the variable.

Why the comprehension scope leaked through

ty models this on purpose (astral-sh/ruff#26466). The walrus's real Definition stays in the comprehension, where its value is inferred, and the name is marked nonlocal or global there. The containing scope gets a synthetic binding, which reads resolve back to the walrus target (astral-sh/ruff#26476). symbol_binding keyed and described the symbol by definition.file_scope(), which is the comprehension.

Resolve the owning scope before keying

A new owning_symbol starts from a definition in a comprehension and follows the marks outward:

  • global goes to the module.
  • nonlocal goes to the nearest enclosing scope that is neither a comprehension nor a class. This passes a walrus out of nested comprehensions.
  • It keeps climbing while the scope it reaches also marks the name. A walrus in inner, which declares nonlocal seen, binds outer's seen.

The symbol key and qualifiedName both use the scope it returns. Comprehension iteration variables and all other definitions keep their own scope. If the owning scope has no symbol of that name, the comprehension's symbol stays.

Plain includeBindings is unchanged. It gives these targets and the reads they reach no binding at all.

Tests

a_walrus_in_a_comprehension_binds_the_containing_scopes_symbol covers four cases: the two-id split above, a walrus passed out of nested comprehensions, a module-level walrus, and a walrus under nonlocal. Each case was mutation-checked against the line it guards:

  • dropping the global branch fails the module-level case;
  • taking the immediate parent instead of skipping comprehensions fails the nested case;
  • stopping after one step fails the nonlocal case.

PEP 572 binds a walrus target inside a comprehension or generator
expression in the scope containing it. Under includeSymbols the target
was described as a local of the comprehension:

    def gen(xs):
        if any((last := w) for w in xs):
            return last    # m.<locals of function 'gen'>.<locals of generator expression>.last

Where the containing scope also assigns the name, one variable got two
ids: `total = 0` and the read inside the comprehension shared one, the
walrus target and `return total` another.

    def partial(values):
        total = 0
        sums = [total := total + v for v in values]
        return total

ty models this on purpose (astral-sh/ruff#26466). The real definition
stays in the comprehension, where its value is inferred, and the name is
marked `nonlocal` or `global` there. The containing scope gets a
synthetic binding, which reads resolve back to the walrus target. The
symbol key and description took the definition's own scope.

`symbol_binding` now keys and describes such a symbol by the scope that
owns it: the module for `global`, otherwise the nearest enclosing scope
that is not a comprehension, which also covers a walrus passed out of
nested comprehensions. Iteration variables and every other definition
keep their scope.
…rehension

A walrus inside a comprehension in `inner`, which declares the name
`nonlocal`, binds `outer`'s variable. Owner resolution now keeps
climbing while the scope it reaches marks the name `global` or
`nonlocal`, skipping class and comprehension scopes.
@knutwannheden knutwannheden self-assigned this Dec 4, 2026
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Dec 4, 2026
@knutwannheden
knutwannheden merged commit 0c3c643 into main Dec 4, 2026
7 checks passed
@knutwannheden
knutwannheden deleted the comprehension-walrus-symbol-scope branch December 4, 2026 08:30
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Dec 4, 2026
knutwannheden added a commit to openrewrite/rewrite that referenced this pull request Dec 4, 2026
ty-types 1.0.2 gave an assignment expression in a comprehension a symbol
in the comprehension's scope, so one variable split into two symbols and
two Variables:

    total = 0
    sums = [total := total + v for v in values]
    return total

total = 0 and the read inside the comprehension had one symbol, the
target and return total another. 1.0.3 (openrewrite/ty-types#40) binds
the target in the scope around the comprehension, where Python binds it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant