Skip to content

Commit 5ee0426

Browse files
committed
Address review
1 parent 659ae83 commit 5ee0426

File tree

3 files changed

+730
-637
lines changed

3 files changed

+730
-637
lines changed

Grammar/python.gram

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,9 @@ 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=named_expression b=[',' z=named_expression { z }] { _PyAST_Assert(a, b, EXTRA) }
213+
assert_stmt[stmt_ty]:
214+
| invalid_assert_stmt
215+
| 'assert' a=expression b=[',' z=expression { z }] { _PyAST_Assert(a, b, EXTRA) }
214216

215217
import_stmt[stmt_ty]:
216218
| invalid_import
@@ -1297,6 +1299,17 @@ invalid_raise_stmt:
12971299
invalid_del_stmt:
12981300
| 'del' a=star_expressions {
12991301
RAISE_SYNTAX_ERROR_INVALID_TARGET(DEL_TARGETS, a) }
1302+
invalid_assert_stmt:
1303+
| 'assert' a=expression '=' b=expression {
1304+
RAISE_SYNTAX_ERROR_KNOWN_RANGE(
1305+
a, b,
1306+
"cannot assign to %s here. Maybe you meant '==' instead of '='?",
1307+
_PyPegen_get_expr_name(a)) }
1308+
| 'assert' expression ',' 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)) }
13001313
invalid_block:
13011314
| NEWLINE !INDENT { RAISE_INDENTATION_ERROR("expected an indented block") }
13021315
invalid_comprehension:

Lib/test/test_syntax.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2689,6 +2689,16 @@ def f(x: *b)
26892689
26902690
Asserts:
26912691
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+
26922702
>>> assert 1 = 2
26932703
Traceback (most recent call last):
26942704
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
@@ -2711,15 +2721,15 @@ def f(x: *b)
27112721
27122722
>>> assert a = 2
27132723
Traceback (most recent call last):
2714-
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
2724+
SyntaxError: cannot assign to name here. Maybe you meant '==' instead of '='?
27152725
27162726
>>> assert (a = 2)
27172727
Traceback (most recent call last):
27182728
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
27192729
27202730
>>> assert a = b
27212731
Traceback (most recent call last):
2722-
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
2732+
SyntaxError: cannot assign to name here. Maybe you meant '==' instead of '='?
27232733
27242734
>>> assert 1, 1 = b
27252735
Traceback (most recent call last):
@@ -2731,12 +2741,16 @@ def f(x: *b)
27312741
27322742
>>> assert 1, a = 1
27332743
Traceback (most recent call last):
2734-
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
2744+
SyntaxError: cannot assign to name here. Maybe you meant '==' instead of '='?
27352745
27362746
>>> assert 1, (a = 1)
27372747
Traceback (most recent call last):
27382748
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
27392749
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+
27402754
"""
27412755

27422756
import re

0 commit comments

Comments
 (0)