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 @@ -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 \"==\"?") }
Expand Down
6 changes: 4 additions & 2 deletions Lib/test/test_named_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
28 changes: 28 additions & 0 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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)``.
37 changes: 37 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