From 185ce343f1e4bd0e7e3c7da66e242c139900bbd6 Mon Sep 17 00:00:00 2001 From: Devansh Baghla Date: Sun, 7 Sep 2025 23:37:57 +0530 Subject: [PATCH 1/5] Raise error for parenthesized target in walrus expression --- Grammar/python.gram | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Grammar/python.gram b/Grammar/python.gram index d36d55183ce629..6b446fcb5485b9 100644 --- a/Grammar/python.gram +++ b/Grammar/python.gram @@ -1261,6 +1261,9 @@ invalid_expression: RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "t-string: lambda expressions are not allowed without parentheses") } invalid_named_expression(memo): + | '(' a=NAME ')' ':=' expression { + RAISE_SYNTAX_ERROR_KNOWN_LOCATION( + a, "cannot parenthesize target name in assignment expression") } | a=expression ':=' expression { RAISE_SYNTAX_ERROR_KNOWN_LOCATION( a, "cannot use assignment expressions with %s", _PyPegen_get_expr_name(a)) } From d9c911ebcf3d3f756bb451f4c3c7183dffc8cee6 Mon Sep 17 00:00:00 2001 From: Devansh Baghla Date: Mon, 8 Sep 2025 01:58:17 +0530 Subject: [PATCH 2/5] Raise SyntaxError for parenthesized walrus target (#138294) --- ...-09-08-12-30-00.gh-issue-138294.abcd12.rst | 1 + Parser/parser.c | 37 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2025-09-08-12-30-00.gh-issue-138294.abcd12.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-09-08-12-30-00.gh-issue-138294.abcd12.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-09-08-12-30-00.gh-issue-138294.abcd12.rst new file mode 100644 index 00000000000000..896de061c9fe77 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-09-08-12-30-00.gh-issue-138294.abcd12.rst @@ -0,0 +1 @@ +Raise SyntaxError when using parentheses around a walrus target. diff --git a/Parser/parser.c b/Parser/parser.c index c20c368a089b33..a9181d0d295f85 100644 --- a/Parser/parser.c +++ b/Parser/parser.c @@ -21587,6 +21587,7 @@ invalid_expression_rule(Parser *p) } // invalid_named_expression: +// | '(' NAME ')' ':=' expression // | expression ':=' expression // | NAME '=' bitwise_or !('=' | ':=') // | !(list | tuple | genexp | 'True' | 'None' | 'False') bitwise_or '=' bitwise_or !('=' | ':=') @@ -21606,6 +21607,42 @@ invalid_named_expression_rule(Parser *p) return _res; } int _mark = p->mark; + { // '(' NAME ')' ':=' expression + if (p->error_indicator) { + p->level--; + return NULL; + } + D(fprintf(stderr, "%*c> invalid_named_expression[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "'(' NAME ')' ':=' expression")); + Token * _literal; + Token * _literal_1; + Token * _literal_2; + expr_ty a; + expr_ty expression_var; + if ( + (_literal = _PyPegen_expect_token(p, 7)) // token='(' + && + (a = _PyPegen_name_token(p)) // NAME + && + (_literal_1 = _PyPegen_expect_token(p, 8)) // token=')' + && + (_literal_2 = _PyPegen_expect_token(p, 53)) // token=':=' + && + (expression_var = expression_rule(p)) // expression + ) + { + D(fprintf(stderr, "%*c+ invalid_named_expression[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "'(' NAME ')' ':=' expression")); + _res = RAISE_SYNTAX_ERROR_KNOWN_LOCATION ( a , "cannot parenthesize target name in assignment expression" ); + if (_res == NULL && PyErr_Occurred()) { + p->error_indicator = 1; + p->level--; + return NULL; + } + goto done; + } + p->mark = _mark; + D(fprintf(stderr, "%*c%s invalid_named_expression[%d-%d]: %s failed!\n", p->level, ' ', + p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "'(' NAME ')' ':=' expression")); + } { // expression ':=' expression if (p->error_indicator) { p->level--; From bb95d1f8015652a526c357a53f44034495f5aacb Mon Sep 17 00:00:00 2001 From: dbXD320 Date: Mon, 8 Sep 2025 02:13:38 +0530 Subject: [PATCH 3/5] Update Misc/NEWS.d/next/Core_and_Builtins/2025-09-08-12-30-00.gh-issue-138294.abcd12.rst Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com> --- .../2025-09-08-12-30-00.gh-issue-138294.abcd12.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-09-08-12-30-00.gh-issue-138294.abcd12.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-09-08-12-30-00.gh-issue-138294.abcd12.rst index 896de061c9fe77..d74324ea978781 100644 --- a/Misc/NEWS.d/next/Core_and_Builtins/2025-09-08-12-30-00.gh-issue-138294.abcd12.rst +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-09-08-12-30-00.gh-issue-138294.abcd12.rst @@ -1 +1 @@ -Raise SyntaxError when using parentheses around a walrus target. +Raise an improved :exc:`SyntaxError` when using parentheses around a walrus target. From 84fbba0d600f90367d1ec64a202465db2d026581 Mon Sep 17 00:00:00 2001 From: Devansh Baghla Date: Mon, 8 Sep 2025 11:18:43 +0530 Subject: [PATCH 4/5] Add test for parenthesized walrus target --- Lib/test/test_syntax.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Lib/test/test_syntax.py b/Lib/test/test_syntax.py index c52d24219410c2..2dc468a4ce8228 100644 --- a/Lib/test/test_syntax.py +++ b/Lib/test/test_syntax.py @@ -2852,6 +2852,11 @@ def test_assign_del(self): # report "cannot delete name" self._check_error("del a += b", "invalid syntax") + def test_parenthesized_named_expression_target(self): + self._check_error( + "((a) := 1)", + "cannot parenthesize target name in assignment expression") + def test_global_param_err_first(self): source = """if 1: def error(a): From 7eb3d1e47e943013aefc93260b4c681b6a050b1b Mon Sep 17 00:00:00 2001 From: Devansh Baghla Date: Tue, 9 Sep 2025 22:53:40 +0530 Subject: [PATCH 5/5] Move parenthesized walrus target test to docstring in test_syntax --- Lib/test/test_syntax.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_syntax.py b/Lib/test/test_syntax.py index 2dc468a4ce8228..b16953c95950e3 100644 --- a/Lib/test/test_syntax.py +++ b/Lib/test/test_syntax.py @@ -2686,6 +2686,13 @@ def f(x: *b) >>> f(x = 5, *:) Traceback (most recent call last): SyntaxError: Invalid star expression + +Invalid assignment expressions with parenthesized targets: + + >>> ((a) := 1) + Traceback (most recent call last): + ... + SyntaxError: cannot parenthesize target name in assignment expression """ import re @@ -2852,11 +2859,6 @@ def test_assign_del(self): # report "cannot delete name" self._check_error("del a += b", "invalid syntax") - def test_parenthesized_named_expression_target(self): - self._check_error( - "((a) := 1)", - "cannot parenthesize target name in assignment expression") - def test_global_param_err_first(self): source = """if 1: def error(a):