Skip to content

Commit c8f29ad

Browse files
authored
bpo-40769: Allow extra surrounding parentheses for invalid annotated assignment rule (GH-20387)
1 parent 6dcbc24 commit c8f29ad

File tree

3 files changed

+284
-207
lines changed

3 files changed

+284
-207
lines changed

Grammar/python.gram

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -646,8 +646,12 @@ invalid_named_expression:
646646
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
647647
a, "cannot use assignment expressions with %s", _PyPegen_get_expr_name(a)) }
648648
invalid_assignment:
649-
| a=list ':' expression { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "only single target (not list) can be annotated") }
650-
| a=tuple ':' expression { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "only single target (not tuple) can be annotated") }
649+
| a=invalid_ann_assign_target ':' expression {
650+
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
651+
a,
652+
"only single target (not %s) can be annotated",
653+
_PyPegen_get_expr_name(a)
654+
)}
651655
| a=star_named_expression ',' star_named_expressions* ':' expression {
652656
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "only single target (not tuple) can be annotated") }
653657
| a=expression ':' expression {
@@ -661,6 +665,10 @@ invalid_assignment:
661665
"'%s' is an illegal expression for augmented assignment",
662666
_PyPegen_get_expr_name(a)
663667
)}
668+
invalid_ann_assign_target[expr_ty]:
669+
| list
670+
| tuple
671+
| '(' a=invalid_ann_assign_target ')' { a }
664672
invalid_del_stmt:
665673
| 'del' a=star_expressions {
666674
RAISE_SYNTAX_ERROR_INVALID_TARGET(DEL_TARGETS, a) }

Lib/test/test_syntax.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,19 @@
733733
Traceback (most recent call last):
734734
SyntaxError: trailing comma not allowed without surrounding parentheses
735735
736+
>>> (): int
737+
Traceback (most recent call last):
738+
SyntaxError: only single target (not tuple) can be annotated
739+
>>> []: int
740+
Traceback (most recent call last):
741+
SyntaxError: only single target (not list) can be annotated
742+
>>> (()): int
743+
Traceback (most recent call last):
744+
SyntaxError: only single target (not tuple) can be annotated
745+
>>> ([]): int
746+
Traceback (most recent call last):
747+
SyntaxError: only single target (not list) can be annotated
748+
736749
Corner-cases that used to fail to raise the correct error:
737750
738751
>>> def f(*, x=lambda __debug__:0): pass

0 commit comments

Comments
 (0)