Skip to content

Commit 659ae83

Browse files
committed
gh-136616: Improve assert syntax error messages
1 parent b74fb8e commit 659ae83

File tree

4 files changed

+293
-192
lines changed

4 files changed

+293
-192
lines changed

Grammar/python.gram

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ del_stmt[stmt_ty]:
210210

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

213-
assert_stmt[stmt_ty]: 'assert' a=expression b=[',' z=expression { z }] { _PyAST_Assert(a, b, EXTRA) }
213+
assert_stmt[stmt_ty]: 'assert' a=named_expression b=[',' z=named_expression { z }] { _PyAST_Assert(a, b, EXTRA) }
214214

215215
import_stmt[stmt_ty]:
216216
| invalid_import

Lib/test/test_syntax.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2686,6 +2686,57 @@ def f(x: *b)
26862686
>>> f(x = 5, *:)
26872687
Traceback (most recent call last):
26882688
SyntaxError: Invalid star expression
2689+
2690+
Asserts:
2691+
2692+
>>> assert 1 = 2
2693+
Traceback (most recent call last):
2694+
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
2695+
2696+
>>> assert (1 = 2)
2697+
Traceback (most recent call last):
2698+
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
2699+
2700+
>>> assert 'a' = a
2701+
Traceback (most recent call last):
2702+
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
2703+
2704+
>>> assert x[0] = 1
2705+
Traceback (most recent call last):
2706+
SyntaxError: cannot assign to subscript here. Maybe you meant '==' instead of '='?
2707+
2708+
>>> assert (yield a) = 2
2709+
Traceback (most recent call last):
2710+
SyntaxError: cannot assign to yield expression here. Maybe you meant '==' instead of '='?
2711+
2712+
>>> assert a = 2
2713+
Traceback (most recent call last):
2714+
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
2715+
2716+
>>> assert (a = 2)
2717+
Traceback (most recent call last):
2718+
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
2719+
2720+
>>> assert a = b
2721+
Traceback (most recent call last):
2722+
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
2723+
2724+
>>> assert 1, 1 = b
2725+
Traceback (most recent call last):
2726+
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
2727+
2728+
>>> assert 1, (1 = b)
2729+
Traceback (most recent call last):
2730+
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
2731+
2732+
>>> assert 1, a = 1
2733+
Traceback (most recent call last):
2734+
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
2735+
2736+
>>> assert 1, (a = 1)
2737+
Traceback (most recent call last):
2738+
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
2739+
26892740
"""
26902741

26912742
import re
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Improve :exc:`SyntaxError` error messages for invalid :keyword:`assert`
2+
usages.

0 commit comments

Comments
 (0)