From 96469ff87915ca380c461da8603ebda4bfd93898 Mon Sep 17 00:00:00 2001 From: flxamartinez Date: Fri, 13 Nov 2026 00:30:03 -0500 Subject: [PATCH] gh-137570: Improve syntax error for unparenthesized assignment expression in lambda lambda x: foo := 2 used to report 'cannot use assignment expressions with lambda', which suggests that assignment expressions are not allowed in a lambda at all. It now reports 'cannot use named expression without parentheses here', the same message used for assert x := 1. --- Grammar/invalid.gram | 2 + Lib/test/test_named_expressions.py | 2 +- Lib/test/test_syntax.py | 10 +++++ ...-11-13-00-14-16.gh-issue-137570.gJElCf.rst | 2 + Parser/parser_invalid.c | 41 +++++++++++++++++++ 5 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-11-13-00-14-16.gh-issue-137570.gJElCf.rst diff --git a/Grammar/invalid.gram b/Grammar/invalid.gram index f788a8be9ab0de3..dd68522def18d9b 100644 --- a/Grammar/invalid.gram +++ b/Grammar/invalid.gram @@ -85,6 +85,8 @@ invalid_if_expression: RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use dict unpacking on only part of a conditional expression") } invalid_named_expression(memo): + | 'lambda' [lambda_params] ':' a=NAME ':=' b=expression { + RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "cannot use named expression without parentheses here") } | 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_named_expressions.py b/Lib/test/test_named_expressions.py index cf44080670dc2e6..96ac6cf46fe6512 100644 --- a/Lib/test/test_named_expressions.py +++ b/Lib/test/test_named_expressions.py @@ -89,7 +89,7 @@ def test_named_expression_invalid_15(self): code = """(lambda: x := 1)""" with self.assertRaisesRegex(SyntaxError, - "cannot use assignment expressions with lambda"): + "cannot use named expression without parentheses here"): exec(code, {}, {}) def test_named_expression_invalid_16(self): diff --git a/Lib/test/test_syntax.py b/Lib/test/test_syntax.py index 04c759310d3d956..6d0646b0e1fb3c3 100644 --- a/Lib/test/test_syntax.py +++ b/Lib/test/test_syntax.py @@ -2867,6 +2867,16 @@ def f(x: *b) Traceback (most recent call last): SyntaxError: cannot use named expression without parentheses here + >>> lambda x: foo := 2 + Traceback (most recent call last): + SyntaxError: cannot use named expression without parentheses here + + >>> lambda: x := 1 + Traceback (most recent call last): + SyntaxError: cannot use named expression without parentheses here + + >>> f = lambda x: (foo := 2) + >>> assert 1 = 2 = 3 Traceback (most recent call last): SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='? diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-13-00-14-16.gh-issue-137570.gJElCf.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-13-00-14-16.gh-issue-137570.gJElCf.rst new file mode 100644 index 000000000000000..8d8061daf239552 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-13-00-14-16.gh-issue-137570.gJElCf.rst @@ -0,0 +1,2 @@ +Improve the :exc:`SyntaxError` message for an unparenthesized assignment +expression in the body of a :keyword:`lambda`. diff --git a/Parser/parser_invalid.c b/Parser/parser_invalid.c index b306b9869a90dd7..ad1821d9599eb8c 100644 --- a/Parser/parser_invalid.c +++ b/Parser/parser_invalid.c @@ -1601,6 +1601,7 @@ invalid_if_expression_rule(Parser *p) } // invalid_named_expression: +// | 'lambda' lambda_params? ':' NAME ':=' expression // | expression ':=' expression // | NAME '=' bitwise_or !('=' | ':=') // | !(list | tuple | genexp | 'True' | 'None' | 'False') bitwise_or '=' bitwise_or !('=' | ':=') @@ -1620,6 +1621,46 @@ invalid_named_expression_rule(Parser *p) return _res; } int _mark = p->mark; + { // 'lambda' lambda_params? ':' 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, "'lambda' lambda_params? ':' NAME ':=' expression")); + Token * _keyword; + Token * _literal; + Token * _literal_1; + void *_opt_var; + UNUSED(_opt_var); // Silence compiler warnings + expr_ty a; + expr_ty b; + if ( + (_keyword = _PyPegen_expect_token(p, 534)) // token='lambda' + && + (_opt_var = lambda_params_rule(p), !p->error_indicator) // lambda_params? + && + (_literal = _PyPegen_expect_token(p, 11)) // 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_named_expression[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "'lambda' lambda_params? ':' 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_named_expression[%d-%d]: %s failed!\n", p->level, ' ', + p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "'lambda' lambda_params? ':' NAME ':=' expression")); + } { // expression ':=' expression if (p->error_indicator) { p->level--;