[3.8] bpo-40663: Correctly handle annotations with subscripts in ast_unparse.c (GH-20156). (GH-20191)
(cherry picked from commit 2135e10dc7
)
Co-authored-by: Batuhan Taskaya <batuhanosmantaskaya@gmail.com>
This commit is contained in:
parent
0cc7becde0
commit
a4d219b35e
|
@ -265,6 +265,10 @@ class AnnotationsFutureTestCase(unittest.TestCase):
|
|||
eq("dict[str, int]")
|
||||
eq("set[str,]")
|
||||
eq("tuple[str, ...]")
|
||||
eq("tuple[(str, *types)]")
|
||||
eq("tuple[xx:yy, (*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))]``.
|
|
@ -750,6 +750,24 @@ append_ast_ext_slice(_PyUnicodeWriter *writer, slice_ty slice)
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
append_ast_index_slice(_PyUnicodeWriter *writer, slice_ty slice)
|
||||
{
|
||||
int level = PR_TUPLE;
|
||||
expr_ty value = slice->v.Index.value;
|
||||
if (value->kind == Tuple_kind) {
|
||||
for (Py_ssize_t i = 0; i < asdl_seq_LEN(value->v.Tuple.elts); i++) {
|
||||
expr_ty element = asdl_seq_GET(value->v.Tuple.elts, i);
|
||||
if (element->kind == Starred_kind) {
|
||||
++level;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
APPEND_EXPR(value, level);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
append_ast_slice(_PyUnicodeWriter *writer, slice_ty slice)
|
||||
{
|
||||
|
@ -759,8 +777,7 @@ append_ast_slice(_PyUnicodeWriter *writer, slice_ty slice)
|
|||
case ExtSlice_kind:
|
||||
return append_ast_ext_slice(writer, slice);
|
||||
case Index_kind:
|
||||
APPEND_EXPR(slice->v.Index.value, PR_TUPLE);
|
||||
return 0;
|
||||
return append_ast_index_slice(writer, slice);
|
||||
default:
|
||||
PyErr_SetString(PyExc_SystemError,
|
||||
"unexpected slice kind");
|
||||
|
|
Loading…
Reference in New Issue