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

Lukasz Langa lukasz at langa.pl
Tue Sep 12 16:10:18 EDT 2017


> On Sep 11, 2017, at 4:21 PM, Yury Selivanov <yselivanov.ml at gmail.com> wrote:
> 
> 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.

I'm not very thrilled about this because lazy evaluation is subject to the new scoping rules (can't use local state) and might give a different result than before.  It's not backwards compatible.  A __future__ import makes it obvious that behavior is going to be different.

And lazy evaluation is an unnecessary step if `get_type_hints()` is used later on so it's unnecessary for the most common usage of annotations.

Finally, I don't think we ever had a "# pragma" suggestion coming from CPython. In reality, people wouldn't bother putting it in most files so tools would have to assume that forward references are correct *and* avoid raising errors about invalid names used in annotations. This is loss of functionality.

- Ł
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 842 bytes
Desc: Message signed with OpenPGP
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170912/c843c2ec/attachment.sig>


More information about the Python-ideas mailing list