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)) } diff --git a/Lib/test/test_syntax.py b/Lib/test/test_syntax.py index c52d24219410c2..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 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..d74324ea978781 --- /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 an improved :exc:`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--;