Skip to content

Commit 71c70d4

Browse files
Commit
1 parent 8b5ce31 commit 71c70d4

3 files changed

Lines changed: 1595 additions & 838 deletions

File tree

Grammar/python.gram

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1212,7 +1212,7 @@ invalid_arguments:
12121212
| a=args ',' args { _PyPegen_arguments_parsing_error(p, a) }
12131213
invalid_kwarg:
12141214
| a[Token*]=('True'|'False'|'None') b='=' {
1215-
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "cannot assign to %s", PyBytes_AS_STRING(a->bytes)) }
1215+
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "cannot assign to reserved keyword '%s'", PyBytes_AS_STRING(a->bytes)) }
12161216
| a=NAME b='=' expression for_if_clauses {
12171217
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '==' or ':=' instead of '='?")}
12181218
| !(NAME '=') a=expression b='=' {
@@ -1273,6 +1273,19 @@ invalid_named_expression(memo):
12731273
_PyPegen_get_expr_name(a)) }
12741274

12751275
invalid_assignment:
1276+
# 'yield' is handled below
1277+
| a[Token*]=('False' | 'None' | 'True' | 'and' | 'as' | 'assert' | 'async'
1278+
| 'await' | 'break' | 'class' | 'continue' | 'def' | 'del'
1279+
| 'elif' | 'else' | 'except' | 'finally' | 'for' | 'from'
1280+
| 'global' | 'if' | 'import' | 'in' | 'is' | 'lambda'
1281+
| 'nonlocal' | 'not' | 'or' | 'pass' | 'raise' | 'return'
1282+
| 'try' | 'while' | 'with') '=' b=expression {
1283+
RAISE_SYNTAX_ERROR_KNOWN_RANGE(
1284+
a,
1285+
b,
1286+
"cannot assign to reserved keyword '%s'",
1287+
PyBytes_AS_STRING(a->bytes)
1288+
)}
12761289
| a=invalid_ann_assign_target ':' expression {
12771290
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
12781291
a,

Lib/test/test_syntax.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,23 @@
3333
3434
>>> None = 1
3535
Traceback (most recent call last):
36-
SyntaxError: cannot assign to None
36+
SyntaxError: cannot assign to reserved keyword 'None'
3737
3838
>>> obj.True = 1
3939
Traceback (most recent call last):
4040
SyntaxError: invalid syntax
4141
4242
>>> True = 1
4343
Traceback (most recent call last):
44-
SyntaxError: cannot assign to True
44+
SyntaxError: cannot assign to reserved keyword 'True'
45+
46+
>>> class = "Mammalia"
47+
Traceback (most recent call last):
48+
SyntaxError: cannot assign to reserved keyword 'class'
49+
50+
>>> lambda = "λ"
51+
Traceback (most recent call last):
52+
SyntaxError: cannot assign to reserved keyword 'lambda'
4553
4654
>>> (True := 1)
4755
Traceback (most recent call last):
@@ -190,7 +198,11 @@
190198
191199
>>> True = True = 3
192200
Traceback (most recent call last):
193-
SyntaxError: cannot assign to True
201+
SyntaxError: cannot assign to reserved keyword 'True'
202+
203+
>>> class = True = 3
204+
Traceback (most recent call last):
205+
SyntaxError: cannot assign to reserved keyword 'class'
194206
195207
>>> x = y = True = z = 3
196208
Traceback (most recent call last):
@@ -810,13 +822,13 @@
810822
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
811823
>>> f(True=1)
812824
Traceback (most recent call last):
813-
SyntaxError: cannot assign to True
825+
SyntaxError: cannot assign to reserved keyword 'True'
814826
>>> f(False=1)
815827
Traceback (most recent call last):
816-
SyntaxError: cannot assign to False
828+
SyntaxError: cannot assign to reserved keyword 'False'
817829
>>> f(None=1)
818830
Traceback (most recent call last):
819-
SyntaxError: cannot assign to None
831+
SyntaxError: cannot assign to reserved keyword 'None'
820832
>>> f(__debug__=1)
821833
Traceback (most recent call last):
822834
SyntaxError: cannot assign to __debug__

0 commit comments

Comments
 (0)