]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-40663: Correctly handle annotations with subscripts in ast_unparse.c (GH-20156)
authorBatuhan Taskaya <batuhanosmantaskaya@gmail.com>
Mon, 18 May 2020 18:23:48 +0000 (21:23 +0300)
committerGitHub <noreply@github.com>
Mon, 18 May 2020 18:23:48 +0000 (19:23 +0100)
Lib/test/test_future.py
Misc/NEWS.d/next/Core and Builtins/2020-05-17-20-38-12.bpo-40663.u2aiZf.rst [new file with mode: 0644]
Python/ast_unparse.c

index 56b7ac6865559469b44872c0ff24d1dc0b723edd..0f40357b3a7310aec2eff565ed4bce058595fb8a 100644 (file)
@@ -275,6 +275,9 @@ class AnnotationsFutureTestCase(unittest.TestCase):
         eq("dict[str, int]")
         eq("set[str,]")
         eq("tuple[str, ...]")
+        eq("tuple[(str, *types)]")
+        eq("tuple[str, int, (str, int)]")
+        eq("tuple[(*int, str, str, (str, int))]")
         eq("tuple[str, int, float, dict[str, int]]")
         eq("slice[0]")
         eq("slice[0:1]")
diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-05-17-20-38-12.bpo-40663.u2aiZf.rst b/Misc/NEWS.d/next/Core and Builtins/2020-05-17-20-38-12.bpo-40663.u2aiZf.rst
new file mode 100644 (file)
index 0000000..5041abc
--- /dev/null
@@ -0,0 +1,2 @@
+Correctly generate annotations where parentheses are omitted but required
+(e.g: ``Type[(str, int, *other))]``.
index d1e9d42d33bd4334055ba4bdac76c45b20a50cb7..e699751a05a0559842d2f94d506092a6b3ffaf58 100644 (file)
@@ -781,8 +781,19 @@ static int
 append_ast_subscript(_PyUnicodeWriter *writer, expr_ty e)
 {
     APPEND_EXPR(e->v.Subscript.value, PR_ATOM);
+    int level = PR_TUPLE;
+    expr_ty slice = e->v.Subscript.slice;
+    if (slice->kind == Tuple_kind) {
+        for (Py_ssize_t i = 0; i < asdl_seq_LEN(slice->v.Tuple.elts); i++) {
+            expr_ty element = asdl_seq_GET(slice->v.Tuple.elts, i);
+            if (element->kind == Starred_kind) {
+                ++level;
+                break;
+            }
+        }
+    }
     APPEND_STR("[");
-    APPEND_EXPR(e->v.Subscript.slice, PR_TUPLE);
+    APPEND_EXPR(e->v.Subscript.slice, level);
     APPEND_STR_FINISH("]");
 }