bpo-40663: Correctly handle annotations with subscripts in ast_unparse.c (GH-20156)

This commit is contained in:
Batuhan Taskaya 2020-05-18 21:23:48 +03:00 committed by GitHub
parent e6578a226d
commit 2135e10dc7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 1 deletions

View 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]")

View File

@ -0,0 +1,2 @@
Correctly generate annotations where parentheses are omitted but required
(e.g: ``Type[(str, int, *other))]``.

View 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("]");
}