Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ 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]: 'assert' a=named_expression b=[',' z=named_expression { z }] { _PyAST_Assert(a, b, EXTRA) }

import_stmt[stmt_ty]:
| invalid_import
Expand Down
51 changes: 51 additions & 0 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -2686,6 +2686,57 @@ def f(x: *b)
>>> f(x = 5, *:)
Traceback (most recent call last):
SyntaxError: Invalid star expression

Asserts:

>>> 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: invalid syntax. Maybe you meant '==' or ':=' 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: invalid syntax. Maybe you meant '==' or ':=' 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: invalid syntax. Maybe you meant '==' or ':=' instead of '='?

>>> assert 1, (a = 1)
Traceback (most recent call last):
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' 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