summaryrefslogtreecommitdiffstats
path: root/Doc/reference
diff options
context:
space:
mode:
authorGuido 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)
commit95e4d589137260530e18ef98a2ed84ee3ec57e12 (patch)
tree9d0c3bc48158e9f0c83f1b9cb509c1fbebd9cfde /Doc/reference
parentd7773d92bd11640a8c950d6c36a9cef1cee36f96 (diff)
downloadcpython-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/reference')
-rw-r--r--Doc/reference/compound_stmts.rst25
-rw-r--r--Doc/reference/simple_stmts.rst15
2 files changed, 28 insertions, 12 deletions
diff --git a/Doc/reference/compound_stmts.rst b/Doc/reference/compound_stmts.rst
index dca9362..d7792f1 100644
--- a/Doc/reference/compound_stmts.rst
+++ b/Doc/reference/compound_stmts.rst
@@ -559,12 +559,14 @@ Parameters may have annotations of the form "``: expression``" following the
parameter name. Any parameter may have an annotation even those of the form
``*identifier`` or ``**identifier``. Functions may have "return" annotation of
the form "``-> expression``" after the parameter list. These annotations can be
-any valid Python expression and are evaluated when the function definition is
-executed. Annotations may be evaluated in a different order than they appear in
-the source code. The presence of annotations does not change the semantics of a
-function. The annotation values are available as values of a dictionary keyed
-by the parameters' names in the :attr:`__annotations__` attribute of the
-function object.
+any valid Python expression. The presence of annotations does not change the
+semantics of a function. The annotation values are available as values of
+a dictionary keyed by the parameters' names in the :attr:`__annotations__`
+attribute of the function object. If the ``annotations`` import from
+:mod:`__future__` is used, annotations are preserved as strings at runtime which
+enables postponed evaluation. Otherwise, they are evaluated when the function
+definition is executed. In this case annotations may be evaluated in
+a different order than they appear in the source code.
.. index:: pair: lambda; expression
@@ -587,6 +589,17 @@ access the local variables of the function containing the def. See section
:pep:`3107` - Function Annotations
The original specification for function annotations.
+ :pep:`484` - Type Hints
+ Definition of a standard meaning for annotations: type hints.
+
+ :pep:`526` - Syntax for Variable Annotations
+ Ability to type hint variable declarations, including class
+ variables and instance variables
+
+ :pep:`563` - Postponed Evaluation of Annotations
+ Support for forward references within annotations by preserving
+ annotations in a string form at runtime instead of eager evaluation.
+
.. _class:
diff --git a/Doc/reference/simple_stmts.rst b/Doc/reference/simple_stmts.rst
index 8d17383..ef9a5f0 100644
--- a/Doc/reference/simple_stmts.rst
+++ b/Doc/reference/simple_stmts.rst
@@ -853,12 +853,15 @@ can appear before a future statement are:
* blank lines, and
* other future statements.
-.. XXX change this if future is cleaned out
-
-The features recognized by Python 3.0 are ``absolute_import``, ``division``,
-``generators``, ``unicode_literals``, ``print_function``, ``nested_scopes`` and
-``with_statement``. They are all redundant because they are always enabled, and
-only kept for backwards compatibility.
+The only feature in Python 3.7 that requires using the future statement is
+``annotations``.
+
+All historical features enabled by the future statement are still recognized
+by Python 3. The list includes ``absolute_import``, ``division``,
+``generators``, ``generator_stop``, ``unicode_literals``,
+``print_function``, ``nested_scopes`` and ``with_statement``. They are
+all redundant because they are always enabled, and only kept for
+backwards compatibility.
A future statement is recognized and treated specially at compile time: Changes
to the semantics of core constructs are often implemented by generating