diff options
author | Guido van Rossum <guido@python.org> | 2018-01-26 16:20:18 (GMT) |
---|---|---|
committer | Łukasz Langa <lukasz@langa.pl> | 2018-01-26 16:20:18 (GMT) |
commit | 95e4d589137260530e18ef98a2ed84ee3ec57e12 (patch) | |
tree | 9d0c3bc48158e9f0c83f1b9cb509c1fbebd9cfde /Doc/whatsnew | |
parent | d7773d92bd11640a8c950d6c36a9cef1cee36f96 (diff) | |
download | cpython-95e4d589137260530e18ef98a2ed84ee3ec57e12.zip cpython-95e4d589137260530e18ef98a2ed84ee3ec57e12.tar.gz cpython-95e4d589137260530e18ef98a2ed84ee3ec57e12.tar.bz2 |
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.
Diffstat (limited to 'Doc/whatsnew')
-rw-r--r-- | Doc/whatsnew/3.7.rst | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/Doc/whatsnew/3.7.rst b/Doc/whatsnew/3.7.rst index 0e1714e..a350919 100644 --- a/Doc/whatsnew/3.7.rst +++ b/Doc/whatsnew/3.7.rst @@ -179,6 +179,57 @@ a normal ``__getattr__`` method, except that it will be defined on module PEP written and implemented by Ivan Levkivskyi +PEP 563: Postponed evaluation of annotations +-------------------------------------------- + +The advent of type hints in Python uncovered two glaring usability issues +with the functionality of annotations added in :pep:`3107` and refined +further in :pep:`526`: + +* annotations could only use names which were already available in the + current scope, in other words they didn't support forward references + of any kind; and + +* annotating source code had adverse effects on startup time of Python + programs. + +Both of these issues are fixed by postponing the evaluation of +annotations. Instead of compiling code which executes expressions in +annotations at their definition time, the compiler stores the annotation +in a string form equivalent to the AST of the expression in question. +If needed, annotations can be resolved at runtime using +``typing.get_type_hints()``. In the common case where this is not +required, the annotations are cheaper to store (since short strings +are interned by the interpreter) and make startup time faster. + +Usability-wise, annotations now support forward references, making the +following syntax valid:: + + class C: + @classmethod + def from_string(cls, source: str) -> C: + ... + + def validate_b(self, obj: B) -> bool: + ... + + class B: + ... + +Since this change breaks compatibility, the new behavior can be enabled +on a per-module basis in Python 3.7 using a ``__future__`` import, like +this:: + + from __future__ import annotations + +It will become the default in Python 4.0. + +.. seealso:: + + :pep:`563` -- Postponed evaluation of annotations + PEP written and implemented by Łukasz Langa. + + PEP 564: Add new time functions with nanosecond resolution ---------------------------------------------------------- |