From 706e1b354c2ff7e8d95dd1aff637ac999697a4a1 Mon Sep 17 00:00:00 2001 From: araooz Date: Fri, 13 Nov 2026 01:56:32 -0500 Subject: [PATCH] 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 --- Grammar/invalid.gram | 2 + Lib/test/test_named_expressions.py | 6 ++- Lib/test/test_syntax.py | 28 ++++++++++++++ ...-11-11-22-00-59.gh-issue-146601.Lyy72t.rst | 3 ++ Parser/parser_invalid.c | 37 +++++++++++++++++++ 5 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-11-11-22-00-59.gh-issue-146601.Lyy72t.rst diff --git a/Grammar/invalid.gram b/Grammar/invalid.gram index f788a8be9ab0de..8b8171432728f1 100644 --- a/Grammar/invalid.gram +++ b/Grammar/invalid.gram @@ -32,6 +32,8 @@ invalid_kwarg: RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "cannot assign to None") } | a=NAME b='=' expression for_if_clauses { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '==' or ':=' instead of '='?")} + | NAME '=' a=NAME ':=' b=expression { + RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "cannot use named expression without parentheses here") } | !(NAME '=') a=expression b='=' { RAISE_SYNTAX_ERROR_KNOWN_RANGE( a, b, "expression cannot contain assignment, perhaps you meant \"==\"?") } diff --git a/Lib/test/test_named_expressions.py b/Lib/test/test_named_expressions.py index cf44080670dc2e..672cef7c28d898 100644 --- a/Lib/test/test_named_expressions.py +++ b/Lib/test/test_named_expressions.py @@ -49,13 +49,15 @@ def test_named_expression_invalid_08(self): def test_named_expression_invalid_09(self): code = """spam(a=b := 'c')""" - with self.assertRaisesRegex(SyntaxError, "invalid syntax"): + with self.assertRaisesRegex(SyntaxError, + "cannot use named expression without parentheses here"): exec(code, {}, {}) def test_named_expression_invalid_10(self): code = """spam(x = y := f(x))""" - with self.assertRaisesRegex(SyntaxError, "invalid syntax"): + with self.assertRaisesRegex(SyntaxError, + "cannot use named expression without parentheses here"): exec(code, {}, {}) def test_named_expression_invalid_11(self): diff --git a/Lib/test/test_syntax.py b/Lib/test/test_syntax.py index 04c759310d3d95..44d95b1f1c56db 100644 --- a/Lib/test/test_syntax.py +++ b/Lib/test/test_syntax.py @@ -2923,6 +2923,34 @@ def f(x: *b) Traceback (most recent call last): SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='? +Named expressions in keyword arguments: + + >>> def g(): return f(a=(x := 1)) # ok + + >>> f(a=x := 1) + Traceback (most recent call last): + SyntaxError: cannot use named expression without parentheses here + + >>> f(1, a=x := 1, b=2) + Traceback (most recent call last): + SyntaxError: cannot use named expression without parentheses here + + >>> f(*args, a=x := 1) + Traceback (most recent call last): + SyntaxError: cannot use named expression without parentheses here + + >>> f(**kwargs, a=x := 1) + Traceback (most recent call last): + SyntaxError: cannot use named expression without parentheses here + + >>> class C(metaclass=x := type): pass + Traceback (most recent call last): + SyntaxError: cannot use named expression without parentheses here + + >>> f(a=x.y := 1) + Traceback (most recent call last): + SyntaxError: invalid syntax + """ import re diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-11-22-00-59.gh-issue-146601.Lyy72t.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-11-22-00-59.gh-issue-146601.Lyy72t.rst new file mode 100644 index 00000000000000..8925c9bf1533e9 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-11-22-00-59.gh-issue-146601.Lyy72t.rst @@ -0,0 +1,3 @@ +Improve the :exc:`SyntaxError` message for an unparenthesized assignment +expression used as the value of a keyword argument, such as +``f(a=x := 1)``. diff --git a/Parser/parser_invalid.c b/Parser/parser_invalid.c index b306b9869a90dd..a9004b5d42c80f 100644 --- a/Parser/parser_invalid.c +++ b/Parser/parser_invalid.c @@ -794,6 +794,7 @@ invalid_arguments_rule(Parser *p) // | 'False' '=' // | 'None' '=' // | NAME '=' expression for_if_clauses +// | NAME '=' NAME ':=' expression // | !(NAME '=') expression '=' // | '**' expression '=' expression Py_LOCAL_SYMBOL void * @@ -922,6 +923,42 @@ invalid_kwarg_rule(Parser *p) D(fprintf(stderr, "%*c%s invalid_kwarg[%d-%d]: %s failed!\n", p->level, ' ', p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "NAME '=' expression for_if_clauses")); } + { // NAME '=' NAME ':=' expression + if (p->error_indicator) { + p->level--; + return NULL; + } + D(fprintf(stderr, "%*c> invalid_kwarg[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "NAME '=' NAME ':=' expression")); + Token * _literal; + Token * _literal_1; + expr_ty a; + expr_ty b; + expr_ty name_var; + if ( + (name_var = _PyPegen_name_token(p)) // NAME + && + (_literal = _PyPegen_expect_token(p, 22)) // token='=' + && + (a = _PyPegen_name_token(p)) // NAME + && + (_literal_1 = _PyPegen_expect_token(p, 53)) // token=':=' + && + (b = expression_rule(p)) // expression + ) + { + D(fprintf(stderr, "%*c+ invalid_kwarg[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "NAME '=' NAME ':=' expression")); + _res = RAISE_SYNTAX_ERROR_KNOWN_RANGE ( a , b , "cannot use named expression without parentheses here" ); + if ((_res == NULL || p->error_indicator) && PyErr_Occurred()) { + p->error_indicator = 1; + p->level--; + return NULL; + } + goto done; + } + p->mark = _mark; + D(fprintf(stderr, "%*c%s invalid_kwarg[%d-%d]: %s failed!\n", p->level, ' ', + p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "NAME '=' NAME ':=' expression")); + } { // !(NAME '=') expression '=' if (p->error_indicator) { p->level--;