Skip to content

gh-136616: Improve assert syntax error messages #136653

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
15 changes: 14 additions & 1 deletion Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,9 @@ del_stmt[stmt_ty]:

yield_stmt[stmt_ty]: y=yield_expr { _PyAST_Expr(y, EXTRA) }

assert_stmt[stmt_ty]: 'assert' a=expression b=[',' z=expression { z }] { _PyAST_Assert(a, b, EXTRA) }
assert_stmt[stmt_ty]:
| invalid_assert_stmt
| 'assert' a=expression b=[',' z=expression { z }] { _PyAST_Assert(a, b, EXTRA) }

import_stmt[stmt_ty]:
| invalid_import
Expand Down Expand Up @@ -1297,6 +1299,17 @@ invalid_raise_stmt:
invalid_del_stmt:
| 'del' a=star_expressions {
RAISE_SYNTAX_ERROR_INVALID_TARGET(DEL_TARGETS, a) }
invalid_assert_stmt:
| 'assert' a=expression '=' b=expression {
RAISE_SYNTAX_ERROR_KNOWN_RANGE(
a, b,
"cannot assign to %s here. Maybe you meant '==' instead of '='?",
_PyPegen_get_expr_name(a)) }
| 'assert' expression ',' a=expression '=' b=expression {
RAISE_SYNTAX_ERROR_KNOWN_RANGE(
a, b,
"cannot assign to %s here. Maybe you meant '==' instead of '='?",
_PyPegen_get_expr_name(a)) }
invalid_block:
| NEWLINE !INDENT { RAISE_INDENTATION_ERROR("expected an indented block") }
invalid_comprehension:
Expand Down
65 changes: 65 additions & 0 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -2686,6 +2686,71 @@ def f(x: *b)
>>> f(x = 5, *:)
Traceback (most recent call last):
SyntaxError: Invalid star expression

Asserts:

>>> assert (a := 1) # ok
>>> # TODO(@sobolevn): improve this message in the next PR
>>> assert a := 1
Traceback (most recent call last):
SyntaxError: invalid syntax

>>> assert 1 = 2 = 3
Traceback (most recent call last):
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?

>>> assert 1 = 2
Traceback (most recent call last):
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?

>>> assert (1 = 2)
Traceback (most recent call last):
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?

>>> assert 'a' = a
Traceback (most recent call last):
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?

>>> assert x[0] = 1
Traceback (most recent call last):
SyntaxError: cannot assign to subscript here. Maybe you meant '==' instead of '='?

>>> assert (yield a) = 2
Traceback (most recent call last):
SyntaxError: cannot assign to yield expression here. Maybe you meant '==' instead of '='?

>>> assert a = 2
Traceback (most recent call last):
SyntaxError: cannot assign to name here. Maybe you meant '==' instead of '='?

>>> assert (a = 2)
Traceback (most recent call last):
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?

>>> assert a = b
Traceback (most recent call last):
SyntaxError: cannot assign to name here. Maybe you meant '==' instead of '='?

>>> assert 1, 1 = b
Traceback (most recent call last):
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?

>>> assert 1, (1 = b)
Traceback (most recent call last):
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?

>>> assert 1, a = 1
Traceback (most recent call last):
SyntaxError: cannot assign to name here. Maybe you meant '==' instead of '='?

>>> assert 1, (a = 1)
Traceback (most recent call last):
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?

>>> assert 1 = a, a = 1
Traceback (most recent call last):
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?

"""

import re
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Improve :exc:`SyntaxError` error messages for invalid :keyword:`assert`
usages.
Loading
Loading