fix: keep parentheses around top-level named expressions (walrus)#480
Open
chuenchen309 wants to merge 1 commit into
Open
fix: keep parentheses around top-level named expressions (walrus)#480chuenchen309 wants to merge 1 commit into
chuenchen309 wants to merge 1 commit into
Conversation
Expr.__str__ rendered the top-level expression by iterating flatly, bypassing the precedence machinery that parenthesizes a named expression (walrus, :=) in nested positions. A named expression extracted as a whole value — an attribute value or a parameter default — was therefore rendered without its parentheses, producing invalid Python: `x = n := 1` is a SyntaxError, and `def f(a=b := 1)` likewise. Wrap a top-level ExprNamedExpr in parentheses, matching ast.unparse. Nested occurrences are still parenthesized by their enclosing context, so there is no double-wrapping.
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.
The bug
A top-level named expression (walrus,
:=) — extracted as a whole attribute value or parameter default — is rendered without its surrounding parentheses, producing invalid Python:ast.unparse(the authoritative "valid, faithful Python" renderer) always parenthesizes aNamedExprhere →'(n := 1)'. Griffe drops the parens only at the top level.Root cause
Expr.__str__(packages/griffelib/src/griffe/_internal/expressions.py) renders the top expression by iterating flatly, which bypasses the_yield/precedence logic that adds walrus parentheses in nested positions.ExprNamedExprdeliberately does not self-parenthesize (that would double-wrap when nested), and a top-level expression has no enclosing context to add them — so a standalone walrus never gets parenthesized.This is exactly the gap left by commit
eb85f0d("Make stringified expressions valid and faithful Python"): its property testtest_expressions_stay_valid_and_equivalentembeds(w := 1)in ~26 contexts, but every one wraps the expression — none renders it standalone.The fix
Wrap a top-level
ExprNamedExprin parentheses in__str__, matchingast.unparse. Nested walruses are still parenthesized by their enclosing context (not by__str__), so there is no double-wrapping.Verification
(n := 1)and(b := compute()); nested cases are unchanged ({'a': (w := 1)},foo(k := 2)— single parens / valid)."%s"(the standalone context) to the_expression_contextsproperty test — onmainit fails on exactly[%s-(w := 1)]and nothing else — plus an explicittest_top_level_named_expression_keeps_parentheses.tests/test_expressions.py→ 910 passed / 16 skipped;tests/test_nodes.py→ 141 passed. No regression.Reachability:
X = (n := compute())(module/class attribute via walrus) and the shared-mutable-default idiomdef f(cache=(_cache := {}))are ordinary user code; mkdocstrings renders these extracted strings straight into signatures/attribute docs, so the invalid output is user-visible.Not a duplicate — the only related history, closed #152, is a different build-time
KeyErrorfor a walrus in a comprehension; no open issue/PR concerns top-level walrus rendering.This PR was authored by an AI coding agent (Claude Code) running on this account: the AI found the bug, ran the repro, wrote the test, and wrote this description. The human account holder reviews every change and is accountable for it. The verification above is real and re-runnable from the diff. If this isn't the kind of contribution you want, say so and I'll close it.