String annotations [PEP 563] (#4390)

* Document `from __future__ import annotations`
* Provide plumbing and tests for `from __future__ import annotations`
* Implement unparsing the AST back to string form

This is required for PEP 563 and as such only implements a part of the
unparsing process that covers expressions.
This commit is contained in:
Guido van Rossum 2018-01-26 08:20:18 -08:00 committed by Łukasz Langa
parent d7773d92bd
commit 95e4d58913
16 changed files with 1476 additions and 29 deletions

View file

@ -1699,13 +1699,30 @@ error:
return 0;
}
static int
compiler_visit_annexpr(struct compiler *c, expr_ty annotation)
{
PyObject *ann_as_str;
ann_as_str = _PyAST_ExprAsUnicode(annotation, 1);
if (!ann_as_str) {
return 0;
}
ADDOP_N(c, LOAD_CONST, ann_as_str, consts);
return 1;
}
static int
compiler_visit_argannotation(struct compiler *c, identifier id,
expr_ty annotation, PyObject *names)
{
if (annotation) {
PyObject *mangled;
VISIT(c, expr, annotation);
if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
VISIT(c, annexpr, annotation)
}
else {
VISIT(c, expr, annotation);
}
mangled = _Py_Mangle(c->u->u_private, id);
if (!mangled)
return 0;
@ -4688,7 +4705,12 @@ compiler_annassign(struct compiler *c, stmt_ty s)
if (!mangled) {
return 0;
}
VISIT(c, expr, s->v.AnnAssign.annotation);
if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
VISIT(c, annexpr, s->v.AnnAssign.annotation)
}
else {
VISIT(c, expr, s->v.AnnAssign.annotation);
}
/* ADDOP_N decrefs its argument */
ADDOP_N(c, STORE_ANNOTATION, mangled, names);
}