You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Specialize syntax error for assignment expressions in invalid places #146601
diff --git a/Grammar/python.gram b/Grammar/python.gram
index 3a91d426c36..75ebf0dc7e3 100644
--- a/Grammar/python.gram+++ b/Grammar/python.gram@@ -1273,6 +1273,7 @@ invalid_expression:
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "f-string: lambda expressions are not allowed without parentheses") }
| a='lambda' [lambda_params] b=':' &TSTRING_MIDDLE {
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "t-string: lambda expressions are not allowed without parentheses") }
+ | a=NAME ':=' b=expression { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "assignment expression must be parenthesized here") }
invalid_if_expression:
| disjunction 'if' b=disjunction 'else' a='*' {
This works well, but I worry it might be overly broad. There's a few invalid cases where this overrides the existing error:
lambda: x:=11ifTrueelsex:=1
These currently report:
File "<python-input-0>", line 1lambda: x:=1^^^^^^^^^SyntaxError: cannot use assignment expressions with lambda
File "<python-input-1>", line 11ifTrueelse x :=1^^^^^^^^^^^^^^^^SyntaxError: cannot use assignment expressions with conditional expression
But with the above patch would report:
>>> lambda: x:=1
File "<python-input-0>", line 1lambda: x:=1^^^^SyntaxError: assignment expression must be parenthesized here
>>> 1 if True else x := 1
File "<python-input-1>", line 11ifTrueelse x :=1^^^^^^SyntaxError: assignment expression must be parenthesized here
I think this is arguably an improvement (it's more likely that the user intended an inner assignment expression than assigning to a non-identifier LHS). But I am little hesitant, since this isn't actually representative of the first pass parser state. (Note that this only applies to sub expressions that start with an assignment expression; cases like lambda: x and y:=1 are unaffected).
This also wouldn't help in a handful of places that don't take an expression (for example, 1 if x:=1 else 2 and [a for a in x:=1]).
@pablogsal@lysnikolaou do you think the changes to the existing syntax errors shown above would be acceptable? If so I can open a PR to show what the broader impact might look like.
I opened #161963, which covers the keyword-argument case from the original report
(f(arg=x := foo())).
@brianschubert I took a narrower approach than your patch: the rule is in invalid_kwarg rather than invalid_expression, so the existing lambda and conditional-expression messages stay as they are, which avoids the question you raised. The lambda case is being handled separately in #161927 (gh-137570), and the other places listed in the docs (with statements, assignment statements, comprehension-if) could be handled in follow-ups.
Activity
brianschubert commentedon Mar 29, 2026
I tried implementing this with this patch:
This works well, but I worry it might be overly broad. There's a few invalid cases where this overrides the existing error:
These currently report:
But with the above patch would report:
I think this is arguably an improvement (it's more likely that the user intended an inner assignment expression than assigning to a non-identifier LHS). But I am little hesitant, since this isn't actually representative of the first pass parser state. (Note that this only applies to sub expressions that start with an assignment expression; cases like
lambda: x and y:=1are unaffected).This also wouldn't help in a handful of places that don't take an
expression(for example,1 if x:=1 else 2and[a for a in x:=1]).@pablogsal @lysnikolaou do you think the changes to the existing syntax errors shown above would be acceptable? If so I can open a PR to show what the broader impact might look like.
araooz commentedon Nov 14, 2026
I opened #161963, which covers the keyword-argument case from the original report
(
f(arg=x := foo())).@brianschubert I took a narrower approach than your patch: the rule is in
invalid_kwargrather thaninvalid_expression, so the existing lambda and conditional-expression messages stay as they are, which avoids the question you raised. The lambda case is being handled separately in #161927 (gh-137570), and the other places listed in the docs (with statements, assignment statements, comprehension-if) could be handled in follow-ups.