[Python-ideas] Delay evaluation of annotations

אלעזר elazarg at gmail.com
Thu Sep 22 13:19:12 EDT 2016


Hi all,

Annotations of function parameters and variables are evaluated when
encountered. This makes it necessary to use string representation for names
that are not yet bound, which affects almost every class definition. It is
also easy to forget, and the result might be a (very uninteresting)
exception in certain untested paths, e.g. inside functions. Editors and
IDEs also don't handle it well; for example, PyDev does not consider
string-annotations as an occurrence of the name, and warns about unused
imports.

I propose delaying evaluation of annotation-expressions by either keeping
the AST of the annotation, or turning it implicitly from EXP into "lambda:
EXP". Inspection code that is interested in this information can access it
be calling (or evaluating) it.

It certainly isn't a backward compatible change, but I think it won't
affect too much code. On the positive side, it will make annotated code
much more pleasing to read, will be less surprising for beginners, and will
help editors in syntax highlighting and name lookup.

In short, it will shift the burden of handling type annotations from
standard code to inspection code, which is where I believe it should rest.

While this idea is quite obvious, I've found no clue in PEP-3107 as to why
will it be a bad one (no such "rejected proposal" there), so I raise the
question here.

Elazar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160922/d68d3b7d/attachment.html>


More information about the Python-ideas mailing list