Skip to content

Commit 706e1b3

Browse files
araoozclaude
andcommitted
gh-146601: Improve error message for unparenthesized walrus in keyword arguments
Add an invalid_kwarg alternative so that `f(a=x := 1)` reports "cannot use named expression without parentheses here" instead of the generic "invalid syntax", reusing the message already used for `assert x := 1`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 2c2d801 commit 706e1b3

5 files changed

Lines changed: 74 additions & 2 deletions

File tree

‎Grammar/invalid.gram‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ invalid_kwarg:
3232
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "cannot assign to None") }
3333
| a=NAME b='=' expression for_if_clauses {
3434
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '==' or ':=' instead of '='?")}
35+
| NAME '=' a=NAME ':=' b=expression {
36+
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "cannot use named expression without parentheses here") }
3537
| !(NAME '=') a=expression b='=' {
3638
RAISE_SYNTAX_ERROR_KNOWN_RANGE(
3739
a, b, "expression cannot contain assignment, perhaps you meant \"==\"?") }

‎Lib/test/test_named_expressions.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,15 @@ def test_named_expression_invalid_08(self):
4949
def test_named_expression_invalid_09(self):
5050
code = """spam(a=b := 'c')"""
5151

52-
with self.assertRaisesRegex(SyntaxError, "invalid syntax"):
52+
with self.assertRaisesRegex(SyntaxError,
53+
"cannot use named expression without parentheses here"):
5354
exec(code, {}, {})
5455

5556
def test_named_expression_invalid_10(self):
5657
code = """spam(x = y := f(x))"""
5758

58-
with self.assertRaisesRegex(SyntaxError, "invalid syntax"):
59+
with self.assertRaisesRegex(SyntaxError,
60+
"cannot use named expression without parentheses here"):
5961
exec(code, {}, {})
6062

6163
def test_named_expression_invalid_11(self):

‎Lib/test/test_syntax.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2923,6 +2923,34 @@ def f(x: *b)
29232923
Traceback (most recent call last):
29242924
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
29252925
2926+
Named expressions in keyword arguments:
2927+
2928+
>>> def g(): return f(a=(x := 1)) # ok
2929+
2930+
>>> f(a=x := 1)
2931+
Traceback (most recent call last):
2932+
SyntaxError: cannot use named expression without parentheses here
2933+
2934+
>>> f(1, a=x := 1, b=2)
2935+
Traceback (most recent call last):
2936+
SyntaxError: cannot use named expression without parentheses here
2937+
2938+
>>> f(*args, a=x := 1)
2939+
Traceback (most recent call last):
2940+
SyntaxError: cannot use named expression without parentheses here
2941+
2942+
>>> f(**kwargs, a=x := 1)
2943+
Traceback (most recent call last):
2944+
SyntaxError: cannot use named expression without parentheses here
2945+
2946+
>>> class C(metaclass=x := type): pass
2947+
Traceback (most recent call last):
2948+
SyntaxError: cannot use named expression without parentheses here
2949+
2950+
>>> f(a=x.y := 1)
2951+
Traceback (most recent call last):
2952+
SyntaxError: invalid syntax
2953+
29262954
"""
29272955

29282956
import re
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Improve the :exc:`SyntaxError` message for an unparenthesized assignment
2+
expression used as the value of a keyword argument, such as
3+
``f(a=x := 1)``.

‎Parser/parser_invalid.c‎

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)