[3.9] bpo-40769: Allow extra surrounding parentheses for invalid annotated assignment rule (GH-20387) (GH-21186)
(cherry picked from commit c8f29ad986f8274fc5fbf889bdd2a211878856b9)
diff --git a/Grammar/python.gram b/Grammar/python.gram
index 8159f83..4da17f3 100644
--- a/Grammar/python.gram
+++ b/Grammar/python.gram
@@ -648,8 +648,12 @@
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
a, "cannot use assignment expressions with %s", _PyPegen_get_expr_name(a)) }
invalid_assignment:
- | a=list ':' expression { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "only single target (not list) can be annotated") }
- | a=tuple ':' expression { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "only single target (not tuple) can be annotated") }
+ | a=invalid_ann_assign_target ':' expression {
+ RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
+ a,
+ "only single target (not %s) can be annotated",
+ _PyPegen_get_expr_name(a)
+ )}
| a=star_named_expression ',' star_named_expressions* ':' expression {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "only single target (not tuple) can be annotated") }
| a=expression ':' expression {
@@ -663,6 +667,10 @@
"'%s' is an illegal expression for augmented assignment",
_PyPegen_get_expr_name(a)
)}
+invalid_ann_assign_target[expr_ty]:
+ | list
+ | tuple
+ | '(' a=invalid_ann_assign_target ')' { a }
invalid_del_stmt:
| 'del' a=star_expressions {
RAISE_SYNTAX_ERROR_INVALID_TARGET(DEL_TARGETS, a) }