fix: avoid double-mangling names inside walrus comprehension#9276
Merged
dmadisetti merged 2 commits intomarimo-team:mainfrom Apr 20, 2026
Merged
fix: avoid double-mangling names inside walrus comprehension#9276dmadisetti merged 2 commits intomarimo-team:mainfrom
dmadisetti merged 2 commits intomarimo-team:mainfrom
Conversation
(marimo-team#9274) When a walrus expression at module scope held a comprehension as its value, the comprehension was visited twice by visit_NamedExpr: once explicitly, and again via generic_visit. After marimo-team#8762 extended mangling to nested-scope lookups, the second visit remangled already-mangled private names, producing errors like `NameError: _cell_XXX_cell_XXX_values`. Only visit the target in the non-comprehension branch, since value has already been visited.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a scoping/mangling bug in the AST ScopedVisitor where module-scope walrus expressions whose value is a comprehension could cause names inside that comprehension to be mangled twice (regression for #9274).
Changes:
- Adjust
visit_NamedExprtraversal to avoid re-visitingnode.value(and thus re-entering comprehension scope). - Add a regression test to ensure comprehension-in-walrus values do not produce double-mangled names.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
marimo/_ast/visitor.py |
Prevents double traversal of walrus value by visiting only target in the non-comprehension branch. |
tests/_ast/test_visitor.py |
Adds regression coverage to ensure names in comprehension values are mangled exactly once. |
Collaborator
Bundle ReportChanges will increase total bundle size by 225.07kB (0.9%) ⬆️. This is within the configured threshold ✅ Detailed changes
Affected Assets, Files, and Routes:view changes for bundle: marimo-esmAssets Changed:
|
The bug in marimo-team#9274 is not specific to set comprehensions — any nested scope inside a walrus value (list/gen/dict comps, lambda) trips the double visit in visit_NamedExpr. Add a parametrized regression test so future changes to named-expression handling surface the broader contract.
Collaborator
|
Added the additional regression test from #9277 |
manzt
approved these changes
Apr 20, 2026
Contributor
Author
|
Thanks @manzt ! |
Collaborator
|
thank you! |
dmadisetti
approved these changes
Apr 20, 2026
Collaborator
dmadisetti
left a comment
There was a problem hiding this comment.
CI is flaky, but the runtime and AST tests passed. Thanks @nojaf !
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
Closes #9274
Walrus expressions at module scope that hold a comprehension as their value were having names inside the comprehension mangled twice, producing errors like:
Root cause
In
visit_NamedExpr(marimo/_ast/visitor.py),node.valuewas visited explicitly, and then — when the walrus was not inside a comprehension —self.generic_visit(node)was called, which visits all children of the node and therefore re-visitednode.value. For a walrus whose value is a comprehension (e.g._matches := {k for k, v in _values.items() if v > 1}), this entered the comprehension's scope twice. After #8762 extended private-name mangling to nested-scope lookups, the second visit re-mangled the already-mangled private name, producing the double prefix.Fix
In the non-comprehension branch of
visit_NamedExpr, only visitnode.target— the value was already visited earlier in the method.Reproduction (from the issue)
Before:
NameError: name '_cell_XXX_cell_XXX_values' is not defined. After: runs successfully.📋 Pre-Review Checklist
✅ Merge Checklist