Skip to content

Commit c952132

Browse files
committed
Apply review comments
1 parent 80a727c commit c952132

File tree

8 files changed

+130
-34
lines changed

8 files changed

+130
-34
lines changed

Grammar/python.gram

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -778,8 +778,8 @@ invalid_named_expression:
778778
a, "cannot use assignment expressions with %s", _PyPegen_get_expr_name(a)) }
779779
| a=NAME b='=' bitwise_or !('='|':='|',') {
780780
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(b, "invalid syntax. Maybe you meant '==' or ':=' instead of '='?") }
781-
| !(list|tuple) a=bitwise_or b='=' bitwise_or !('='|':='|',') {
782-
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(b, "cannot assign to %s. Maybe you meant '==' instead of '='?",
781+
| !(list|tuple|genexp|'True'|'None'|'False') a=bitwise_or b='=' bitwise_or !('='|':='|',') {
782+
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(b, "cannot assign to %s here. Maybe you meant '==' instead of '='?",
783783
_PyPegen_get_expr_name(a)) }
784784

785785
invalid_assignment:

Lib/test/test_cmd_line_script.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ def test_syntaxerror_unindented_caret_position(self):
600600
script_name = _make_test_script(script_dir, 'script', script)
601601
exitcode, stdout, stderr = assert_python_failure(script_name)
602602
text = io.TextIOWrapper(io.BytesIO(stderr), 'ascii').read()
603-
# Confirm that the caret is located under the first 1 character
603+
# Confirm that the caret is located under the '=' sign
604604
self.assertIn("\n 1 + 1 = 2\n ^\n", text)
605605

606606
def test_syntaxerror_indented_caret_position(self):

Lib/test/test_fstring.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,7 @@ def test_conversions(self):
990990
])
991991

992992
def test_assignment(self):
993-
self.assertAllRaise(SyntaxError, r'invalid syntax\.',
993+
self.assertAllRaise(SyntaxError, r'invalid syntax',
994994
["f'' = 3",
995995
"f'{0}' = x",
996996
"f'{x}' = x",

Lib/test/test_generators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2013,7 +2013,7 @@ def printsolution(self, x):
20132013
>>> def f(): (yield bar) = y
20142014
Traceback (most recent call last):
20152015
...
2016-
SyntaxError: cannot assign to yield expression. Maybe you meant '==' instead of '='?
2016+
SyntaxError: cannot assign to yield expression here. Maybe you meant '==' instead of '='?
20172017
20182018
>>> def f(): (yield bar) += y
20192019
Traceback (most recent call last):

Lib/test/test_genexps.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@
153153
>>> (y for y in (1,2)) = 10
154154
Traceback (most recent call last):
155155
...
156-
SyntaxError: cannot assign to generator expression. Maybe you meant '==' instead of '='?
156+
SyntaxError: cannot assign to generator expression
157157
158158
>>> (y for y in (1,2)) += 10
159159
Traceback (most recent call last):

Lib/test/test_syntax.py

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@
3333
3434
>>> None = 1
3535
Traceback (most recent call last):
36-
SyntaxError: cannot assign to None. Maybe you meant '==' instead of '='?
36+
SyntaxError: cannot assign to 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. Maybe you meant '==' instead of '='?
44+
SyntaxError: cannot assign to True
4545
4646
>>> (True := 1)
4747
Traceback (most recent call last):
@@ -61,7 +61,7 @@
6161
6262
>>> f() = 1
6363
Traceback (most recent call last):
64-
SyntaxError: cannot assign to function call. Maybe you meant '==' instead of '='?
64+
SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
6565
6666
>>> yield = 1
6767
Traceback (most recent call last):
@@ -73,27 +73,27 @@
7373
7474
>>> a + 1 = 2
7575
Traceback (most recent call last):
76-
SyntaxError: cannot assign to expression. Maybe you meant '==' instead of '='?
76+
SyntaxError: cannot assign to expression here. Maybe you meant '==' instead of '='?
7777
7878
>>> (x for x in x) = 1
7979
Traceback (most recent call last):
80-
SyntaxError: cannot assign to generator expression. Maybe you meant '==' instead of '='?
80+
SyntaxError: cannot assign to generator expression
8181
8282
>>> 1 = 1
8383
Traceback (most recent call last):
84-
SyntaxError: cannot assign to literal. Maybe you meant '==' instead of '='?
84+
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
8585
8686
>>> "abc" = 1
8787
Traceback (most recent call last):
88-
SyntaxError: cannot assign to literal. Maybe you meant '==' instead of '='?
88+
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
8989
9090
>>> b"" = 1
9191
Traceback (most recent call last):
92-
SyntaxError: cannot assign to literal. Maybe you meant '==' instead of '='?
92+
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
9393
9494
>>> ... = 1
9595
Traceback (most recent call last):
96-
SyntaxError: cannot assign to Ellipsis. Maybe you meant '==' instead of '='?
96+
SyntaxError: cannot assign to Ellipsis here. Maybe you meant '==' instead of '='?
9797
9898
>>> `1` = 1
9999
Traceback (most recent call last):
@@ -465,7 +465,7 @@
465465
# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
466466
# >>> f(True=2)
467467
# Traceback (most recent call last):
468-
# SyntaxError: cannot assign to True. Maybe you meant '==' instead of '='?
468+
# SyntaxError: cannot assign to True here. Maybe you meant '==' instead of '='?
469469
>>> f(__debug__=1)
470470
Traceback (most recent call last):
471471
SyntaxError: cannot assign to __debug__
@@ -684,15 +684,15 @@
684684
... pass
685685
Traceback (most recent call last):
686686
...
687-
SyntaxError: cannot assign to function call. Maybe you meant '==' instead of '='?
687+
SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
688688
689689
>>> if 1:
690690
... pass
691691
... elif 1:
692692
... x() = 1
693693
Traceback (most recent call last):
694694
...
695-
SyntaxError: cannot assign to function call. Maybe you meant '==' instead of '='?
695+
SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
696696
697697
>>> if 1:
698698
... x() = 1
@@ -702,7 +702,7 @@
702702
... pass
703703
Traceback (most recent call last):
704704
...
705-
SyntaxError: cannot assign to function call. Maybe you meant '==' instead of '='?
705+
SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
706706
707707
>>> if 1:
708708
... pass
@@ -712,7 +712,7 @@
712712
... pass
713713
Traceback (most recent call last):
714714
...
715-
SyntaxError: cannot assign to function call. Maybe you meant '==' instead of '='?
715+
SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
716716
717717
>>> if 1:
718718
... pass
@@ -722,7 +722,7 @@
722722
... x() = 1
723723
Traceback (most recent call last):
724724
...
725-
SyntaxError: cannot assign to function call. Maybe you meant '==' instead of '='?
725+
SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
726726
727727
Missing ':' before suites:
728728
@@ -843,6 +843,26 @@
843843
Traceback (most recent call last):
844844
SyntaxError: expected ':'
845845
846+
>>> if x = 3:
847+
... pass
848+
Traceback (most recent call last):
849+
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
850+
851+
>>> while x = 3:
852+
... pass
853+
Traceback (most recent call last):
854+
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
855+
856+
>>> if x.a = 3:
857+
... pass
858+
Traceback (most recent call last):
859+
SyntaxError: cannot assign to attribute here. Maybe you meant '==' instead of '='?
860+
861+
>>> while x.a = 3:
862+
... pass
863+
Traceback (most recent call last):
864+
SyntaxError: cannot assign to attribute here. Maybe you meant '==' instead of '='?
865+
846866
Make sure that the old "raise X, Y[, Z]" form is gone:
847867
>>> raise X, Y
848868
Traceback (most recent call last):
@@ -894,19 +914,19 @@
894914
895915
>>> {1, 2, 3} = 42
896916
Traceback (most recent call last):
897-
SyntaxError: cannot assign to set display. Maybe you meant '==' instead of '='?
917+
SyntaxError: cannot assign to set display here. Maybe you meant '==' instead of '='?
898918
899919
>>> {1: 2, 3: 4} = 42
900920
Traceback (most recent call last):
901-
SyntaxError: cannot assign to dict display. Maybe you meant '==' instead of '='?
921+
SyntaxError: cannot assign to dict literal here. Maybe you meant '==' instead of '='?
902922
903923
>>> f'{x}' = 42
904924
Traceback (most recent call last):
905-
SyntaxError: cannot assign to f-string expression. Maybe you meant '==' instead of '='?
925+
SyntaxError: cannot assign to f-string expression here. Maybe you meant '==' instead of '='?
906926
907927
>>> f'{x}-{y}' = 42
908928
Traceback (most recent call last):
909-
SyntaxError: cannot assign to f-string expression. Maybe you meant '==' instead of '='?
929+
SyntaxError: cannot assign to f-string expression here. Maybe you meant '==' instead of '='?
910930
911931
>>> from t import x,
912932
Traceback (most recent call last):
@@ -988,7 +1008,7 @@ def _check_error(self, code, errtext,
9881008
def test_expression_with_assignment(self):
9891009
self._check_error(
9901010
"print(end1 + end2 = ' ')",
991-
"cannot assign to expression. Maybe you meant '==' instead of '='?",
1011+
"cannot assign to expression here. Maybe you meant '==' instead of '='?",
9921012
offset=19
9931013
)
9941014

Parser/parser.c

Lines changed: 83 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17262,7 +17262,7 @@ invalid_kwarg_rule(Parser *p)
1726217262
// invalid_named_expression:
1726317263
// | expression ':=' expression
1726417264
// | NAME '=' bitwise_or !('=' | ':=' | ',')
17265-
// | !(list | tuple) bitwise_or '=' bitwise_or !('=' | ':=' | ',')
17265+
// | !(list | tuple | genexp | 'True' | 'None' | 'False') bitwise_or '=' bitwise_or !('=' | ':=' | ',')
1726617266
static void *
1726717267
invalid_named_expression_rule(Parser *p)
1726817268
{
@@ -17335,12 +17335,12 @@ invalid_named_expression_rule(Parser *p)
1733517335
D(fprintf(stderr, "%*c%s invalid_named_expression[%d-%d]: %s failed!\n", p->level, ' ',
1733617336
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "NAME '=' bitwise_or !('=' | ':=' | ',')"));
1733717337
}
17338-
{ // !(list | tuple) bitwise_or '=' bitwise_or !('=' | ':=' | ',')
17338+
{ // !(list | tuple | genexp | 'True' | 'None' | 'False') bitwise_or '=' bitwise_or !('=' | ':=' | ',')
1733917339
if (p->error_indicator) {
1734017340
D(p->level--);
1734117341
return NULL;
1734217342
}
17343-
D(fprintf(stderr, "%*c> invalid_named_expression[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "!(list | tuple) bitwise_or '=' bitwise_or !('=' | ':=' | ',')"));
17343+
D(fprintf(stderr, "%*c> invalid_named_expression[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "!(list | tuple | genexp | 'True' | 'None' | 'False') bitwise_or '=' bitwise_or !('=' | ':=' | ',')"));
1734417344
expr_ty a;
1734517345
Token * b;
1734617346
expr_ty bitwise_or_var;
@@ -17356,8 +17356,8 @@ invalid_named_expression_rule(Parser *p)
1735617356
_PyPegen_lookahead(0, _tmp_145_rule, p)
1735717357
)
1735817358
{
17359-
D(fprintf(stderr, "%*c+ invalid_named_expression[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "!(list | tuple) bitwise_or '=' bitwise_or !('=' | ':=' | ',')"));
17360-
_res = RAISE_SYNTAX_ERROR_KNOWN_LOCATION ( b , "cannot assign to %s. Maybe you meant '==' instead of '='?" , _PyPegen_get_expr_name ( a ) );
17359+
D(fprintf(stderr, "%*c+ invalid_named_expression[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "!(list | tuple | genexp | 'True' | 'None' | 'False') bitwise_or '=' bitwise_or !('=' | ':=' | ',')"));
17360+
_res = RAISE_SYNTAX_ERROR_KNOWN_LOCATION ( b , "cannot assign to %s here. Maybe you meant '==' instead of '='?" , _PyPegen_get_expr_name ( a ) );
1736117361
if (_res == NULL && PyErr_Occurred()) {
1736217362
p->error_indicator = 1;
1736317363
D(p->level--);
@@ -17367,7 +17367,7 @@ invalid_named_expression_rule(Parser *p)
1736717367
}
1736817368
p->mark = _mark;
1736917369
D(fprintf(stderr, "%*c%s invalid_named_expression[%d-%d]: %s failed!\n", p->level, ' ',
17370-
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "!(list | tuple) bitwise_or '=' bitwise_or !('=' | ':=' | ',')"));
17370+
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "!(list | tuple | genexp | 'True' | 'None' | 'False') bitwise_or '=' bitwise_or !('=' | ':=' | ',')"));
1737117371
}
1737217372
_res = NULL;
1737317373
done:
@@ -27615,7 +27615,7 @@ _tmp_143_rule(Parser *p)
2761527615
return _res;
2761627616
}
2761727617

27618-
// _tmp_144: list | tuple
27618+
// _tmp_144: list | tuple | genexp | 'True' | 'None' | 'False'
2761927619
static void *
2762027620
_tmp_144_rule(Parser *p)
2762127621
{
@@ -27664,6 +27664,82 @@ _tmp_144_rule(Parser *p)
2766427664
D(fprintf(stderr, "%*c%s _tmp_144[%d-%d]: %s failed!\n", p->level, ' ',
2766527665
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "tuple"));
2766627666
}
27667+
{ // genexp
27668+
if (p->error_indicator) {
27669+
D(p->level--);
27670+
return NULL;
27671+
}
27672+
D(fprintf(stderr, "%*c> _tmp_144[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "genexp"));
27673+
expr_ty genexp_var;
27674+
if (
27675+
(genexp_var = genexp_rule(p)) // genexp
27676+
)
27677+
{
27678+
D(fprintf(stderr, "%*c+ _tmp_144[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "genexp"));
27679+
_res = genexp_var;
27680+
goto done;
27681+
}
27682+
p->mark = _mark;
27683+
D(fprintf(stderr, "%*c%s _tmp_144[%d-%d]: %s failed!\n", p->level, ' ',
27684+
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "genexp"));
27685+
}
27686+
{ // 'True'
27687+
if (p->error_indicator) {
27688+
D(p->level--);
27689+
return NULL;
27690+
}
27691+
D(fprintf(stderr, "%*c> _tmp_144[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "'True'"));
27692+
Token * _keyword;
27693+
if (
27694+
(_keyword = _PyPegen_expect_token(p, 524)) // token='True'
27695+
)
27696+
{
27697+
D(fprintf(stderr, "%*c+ _tmp_144[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "'True'"));
27698+
_res = _keyword;
27699+
goto done;
27700+
}
27701+
p->mark = _mark;
27702+
D(fprintf(stderr, "%*c%s _tmp_144[%d-%d]: %s failed!\n", p->level, ' ',
27703+
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "'True'"));
27704+
}
27705+
{ // 'None'
27706+
if (p->error_indicator) {
27707+
D(p->level--);
27708+
return NULL;
27709+
}
27710+
D(fprintf(stderr, "%*c> _tmp_144[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "'None'"));
27711+
Token * _keyword;
27712+
if (
27713+
(_keyword = _PyPegen_expect_token(p, 523)) // token='None'
27714+
)
27715+
{
27716+
D(fprintf(stderr, "%*c+ _tmp_144[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "'None'"));
27717+
_res = _keyword;
27718+
goto done;
27719+
}
27720+
p->mark = _mark;
27721+
D(fprintf(stderr, "%*c%s _tmp_144[%d-%d]: %s failed!\n", p->level, ' ',
27722+
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "'None'"));
27723+
}
27724+
{ // 'False'
27725+
if (p->error_indicator) {
27726+
D(p->level--);
27727+
return NULL;
27728+
}
27729+
D(fprintf(stderr, "%*c> _tmp_144[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "'False'"));
27730+
Token * _keyword;
27731+
if (
27732+
(_keyword = _PyPegen_expect_token(p, 525)) // token='False'
27733+
)
27734+
{
27735+
D(fprintf(stderr, "%*c+ _tmp_144[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "'False'"));
27736+
_res = _keyword;
27737+
goto done;
27738+
}
27739+
p->mark = _mark;
27740+
D(fprintf(stderr, "%*c%s _tmp_144[%d-%d]: %s failed!\n", p->level, ' ',
27741+
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "'False'"));
27742+
}
2766727743
_res = NULL;
2766827744
done:
2766927745
D(p->level--);

Parser/pegen.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ _PyPegen_get_expr_name(expr_ty e)
199199
case DictComp_kind:
200200
return "dict comprehension";
201201
case Dict_kind:
202-
return "dict display";
202+
return "dict literal";
203203
case Set_kind:
204204
return "set display";
205205
case JoinedStr_kind:

0 commit comments

Comments
 (0)