Repository navigation
Give a comprehension's walrus target the containing scope's symbol - #40
Merged
Merged
Conversation
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.
This was referenced Dec 4, 2026
Merged
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.
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.
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:With this change all four references share one id,
m.<locals of function 'partial'>.total. A module-level walrus in a comprehension readsm.hitrather thanm.<locals of list comprehension>.hit. This matches what the README already promises forglobalandnonlocalnames: 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
Definitionstays in the comprehension, where its value is inferred, and the name is markednonlocalorglobalthere. The containing scope gets a synthetic binding, which reads resolve back to the walrus target (astral-sh/ruff#26476).symbol_bindingkeyed and described the symbol bydefinition.file_scope(), which is the comprehension.Resolve the owning scope before keying
A new
owning_symbolstarts from a definition in a comprehension and follows the marks outward:globalgoes to the module.nonlocalgoes to the nearest enclosing scope that is neither a comprehension nor a class. This passes a walrus out of nested comprehensions.inner, which declaresnonlocal seen, bindsouter'sseen.The symbol key and
qualifiedNameboth 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
includeBindingsis unchanged. It gives these targets and the reads they reach no binding at all.Tests
a_walrus_in_a_comprehension_binds_the_containing_scopes_symbolcovers four cases: the two-id split above, a walrus passed out of nested comprehensions, a module-level walrus, and a walrus undernonlocal. Each case was mutation-checked against the line it guards:globalbranch fails the module-level case;nonlocalcase.