Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.8] bpo-40663: Correctly handle annotations with subscripts in ast_… #20191

Merged
merged 1 commit into from May 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions Lib/test/test_future.py
Expand Up @@ -265,6 +265,10 @@ def test_annotations(self):
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]")
Expand Down
@@ -0,0 +1,2 @@
Correctly generate annotations where parentheses are omitted but required
(e.g: ``Type[(str, int, *other))]``.
21 changes: 19 additions & 2 deletions Python/ast_unparse.c
Expand Up @@ -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)
{
Expand All @@ -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");
Expand Down