Skip to content

Commit 6bc65c3

Browse files
authored
gh-136616: Improve assert syntax error messages (#136653)
1 parent 66ef161 commit 6bc65c3

File tree

4 files changed

+594
-400
lines changed

4 files changed

+594
-400
lines changed

Grammar/python.gram

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,9 @@ del_stmt[stmt_ty]:
212212

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

215-
assert_stmt[stmt_ty]: 'assert' a=expression b=[',' z=expression { z }] { _PyAST_Assert(a, b, EXTRA) }
215+
assert_stmt[stmt_ty]:
216+
| invalid_assert_stmt
217+
| 'assert' a=expression b=[',' z=expression { z }] { _PyAST_Assert(a, b, EXTRA) }
216218

217219
import_stmt[stmt_ty]:
218220
| invalid_import
@@ -1302,6 +1304,17 @@ invalid_raise_stmt:
13021304
invalid_del_stmt:
13031305
| 'del' a=star_expressions {
13041306
RAISE_SYNTAX_ERROR_INVALID_TARGET(DEL_TARGETS, a) }
1307+
invalid_assert_stmt:
1308+
| 'assert' a=expression '=' b=expression {
1309+
RAISE_SYNTAX_ERROR_KNOWN_RANGE(
1310+
a, b,
1311+
"cannot assign to %s here. Maybe you meant '==' instead of '='?",
1312+
_PyPegen_get_expr_name(a)) }
1313+
| 'assert' expression ',' a=expression '=' b=expression {
1314+
RAISE_SYNTAX_ERROR_KNOWN_RANGE(
1315+
a, b,
1316+
"cannot assign to %s here. Maybe you meant '==' instead of '='?",
1317+
_PyPegen_get_expr_name(a)) }
13051318
invalid_block:
13061319
| NEWLINE !INDENT { RAISE_INDENTATION_ERROR("expected an indented block") }
13071320
invalid_comprehension:

Lib/test/test_syntax.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2686,6 +2686,71 @@ 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 (a := 1) # ok
2693+
>>> # TODO(@sobolevn): improve this message in the next PR
2694+
>>> assert a := 1
2695+
Traceback (most recent call last):
2696+
SyntaxError: invalid syntax
2697+
2698+
>>> assert 1 = 2 = 3
2699+
Traceback (most recent call last):
2700+
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
2701+
2702+
>>> assert 1 = 2
2703+
Traceback (most recent call last):
2704+
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
2705+
2706+
>>> assert (1 = 2)
2707+
Traceback (most recent call last):
2708+
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
2709+
2710+
>>> assert 'a' = a
2711+
Traceback (most recent call last):
2712+
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
2713+
2714+
>>> assert x[0] = 1
2715+
Traceback (most recent call last):
2716+
SyntaxError: cannot assign to subscript here. Maybe you meant '==' instead of '='?
2717+
2718+
>>> assert (yield a) = 2
2719+
Traceback (most recent call last):
2720+
SyntaxError: cannot assign to yield expression here. Maybe you meant '==' instead of '='?
2721+
2722+
>>> assert a = 2
2723+
Traceback (most recent call last):
2724+
SyntaxError: cannot assign to name here. Maybe you meant '==' instead of '='?
2725+
2726+
>>> assert (a = 2)
2727+
Traceback (most recent call last):
2728+
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
2729+
2730+
>>> assert a = b
2731+
Traceback (most recent call last):
2732+
SyntaxError: cannot assign to name here. Maybe you meant '==' instead of '='?
2733+
2734+
>>> assert 1, 1 = b
2735+
Traceback (most recent call last):
2736+
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
2737+
2738+
>>> assert 1, (1 = b)
2739+
Traceback (most recent call last):
2740+
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
2741+
2742+
>>> assert 1, a = 1
2743+
Traceback (most recent call last):
2744+
SyntaxError: cannot assign to name here. Maybe you meant '==' instead of '='?
2745+
2746+
>>> assert 1, (a = 1)
2747+
Traceback (most recent call last):
2748+
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
2749+
2750+
>>> assert 1 = a, a = 1
2751+
Traceback (most recent call last):
2752+
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
2753+
26892754
"""
26902755

26912756
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)