gh-138716: Fix assert a := b syntax error message (#138718)

This commit is contained in:
sobolevn
2025-09-10 16:05:16 +03:00
committed by GitHub
parent 1ce05537a3
commit cf8f36fe0a
4 changed files with 262 additions and 173 deletions

View File

@@ -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:

View File

@@ -2690,10 +2690,15 @@ Invalid expressions in type scopes:
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):

View File

@@ -0,0 +1,2 @@
Improve :exc:`SyntaxError` message for :keyword:`assert` in cases like
``assert a := b``.

416
Parser/parser.c generated

File diff suppressed because it is too large Load Diff