Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
3 changes: 3 additions & 0 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -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") }
Comment thread
StanFromIreland marked this conversation as resolved.
| a=expression ':=' expression {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
a, "cannot use assignment expressions with %s", _PyPegen_get_expr_name(a)) }
Expand Down
5 changes: 5 additions & 0 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd be best if this went in the docstring at the top of the file instead of a separate test. It can probably go at the very bottom of that docstring with a short description of the error being tested, something along the lines of Invalid parenthesization inside assignment expressions.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dbXD320 You can add a new section like this:

Invalid expressions in type scopes:

def test_global_param_err_first(self):
source = """if 1:
def error(a):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Raise an improved :exc:`SyntaxError` when using parentheses around a walrus target.
37 changes: 37 additions & 0 deletions Parser/parser.c

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

Loading