Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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
gh-138716: Fix assert a := b syntax error message
  • Loading branch information
sobolevn committed Sep 10, 2025
commit 9bc06a651fe630f11bf6cc1cfddc39649d310dbb
8 changes: 8 additions & 0 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -1315,6 +1315,14 @@ invalid_assert_stmt:
a, b,
"cannot assign to %s here. Maybe you meant '==' instead of '='?",
_PyPegen_get_expr_name(a)) }
| 'assert' a=expression ':=' b=expression {
RAISE_SYNTAX_ERROR_KNOWN_RANGE(
a, b,
"cannot use named expression without parentheses here") }
| 'assert' expression ',' a=expression ':=' b=expression {
RAISE_SYNTAX_ERROR_KNOWN_RANGE(
a, b,
"cannot use named expression without parentheses here") }
invalid_block:
| NEWLINE !INDENT { RAISE_INDENTATION_ERROR("expected an indented block") }
invalid_comprehension:
Expand Down
9 changes: 7 additions & 2 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -2690,10 +2690,15 @@ def f(x: *b)
Asserts:

>>> assert (a := 1) # ok
>>> # TODO(@sobolevn): improve this message in the next PR
>>> assert 1, (a := 1) # ok

>>> assert a := 1
Traceback (most recent call last):
SyntaxError: invalid syntax
SyntaxError: cannot use named expression without parentheses here

>>> assert 1, a := 1
Traceback (most recent call last):
SyntaxError: cannot use named expression without parentheses here

>>> assert 1 = 2 = 3
Traceback (most recent call last):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Improve :exc:`SyntaxError` message for :keyword:`assert` in cases like
``assert a := b``.
Loading
Loading