bpo-40663: Correctly handle annotations with subscripts in ast_unparse.c (GH-20156)
This commit is contained in:
parent
e6578a226d
commit
2135e10dc7
|
@ -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]")
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
Correctly generate annotations where parentheses are omitted but required
|
||||
(e.g: ``Type[(str, int, *other))]``.
|
|
@ -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("]");
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue