[Python-ideas] PEP 563: Postponed Evaluation of Annotations, first draft

Yury Selivanov yselivanov.ml at gmail.com
Mon Sep 11 16:21:04 EDT 2017


On Mon, Sep 11, 2017 at 3:25 PM, Lukasz Langa <lukasz at langa.pl> wrote:
[..]
> This PEP is proposing delaying evaluation until annotations are accessed but
> gives user code the power to decide whether the string form is enough, or
> maybe an AST would be enough, or actual evaluation with get_type_hints() or
> eval() is necessary.

I'm one of those who used annotations for other purposes than type
hints. And even if annotations became strings in Python 3.7 *without
future import*, fixing my libraries would be easy -- just add an
eval().

That said, the PEP doesn't cover an alternative solution:

1. Add another special attribute to functions: __annotations_text__.

2. __annotations__ becomes a dynamic Mapping, which evaluates stuff
from __annotations_text__ *lazily*.

3. Recommend linters and IDEs to support "# pragma: annotations", as a
way to say that the Python files follows the new Python 3.7
annotations semantics.

That would maintain full backwards compatibility with all existing
Python libraries and would not require a future import.

Yury


More information about the Python-ideas mailing list