Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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)) }
Copy link
Member

Choose a reason for hiding this comment

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

Is the second alternative here needed in reality. If someone's using the second argument here and have a comparison there, maybe they're confused and meant something completely different?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hm, good question. They might. For example, assert some.method() == other.method(), some == other might be a realiastic case. But, I am open to other opinions :)

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