Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Grammar/invalid.gram
Original file line number Diff line number Diff line change
Expand Up @@ -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)) }
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_named_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
10 changes: 10 additions & 0 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 '='?
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Improve the :exc:`SyntaxError` message for an unparenthesized assignment
expression in the body of a :keyword:`lambda`.
41 changes: 41 additions & 0 deletions Parser/parser_invalid.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading