Skip to content

Commit 5436d8b

Browse files
JelleZijlstrapicnixzAA-Turnerwillingc
authored
gh-119180: Documentation for PEP 649 and 749 (#122235)
Co-authored-by: Bénédikt Tran <[email protected]> Co-authored-by: Adam Turner <[email protected]> Co-authored-by: Carol Willing <[email protected]>
1 parent 6e23c89 commit 5436d8b

File tree

11 files changed

+680
-104
lines changed

11 files changed

+680
-104
lines changed

Doc/glossary.rst

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,24 @@ Glossary
3636
and loaders (in the :mod:`importlib.abc` module). You can create your own
3737
ABCs with the :mod:`abc` module.
3838

39+
annotate function
40+
A function that can be called to retrieve the :term:`annotations <annotation>`
41+
of an object. This function is accessible as the :attr:`~object.__annotate__`
42+
attribute of functions, classes, and modules. Annotate functions are a
43+
subset of :term:`evaluate functions <evaluate function>`.
44+
3945
annotation
4046
A label associated with a variable, a class
4147
attribute or a function parameter or return value,
4248
used by convention as a :term:`type hint`.
4349

4450
Annotations of local variables cannot be accessed at runtime, but
4551
annotations of global variables, class attributes, and functions
46-
are stored in the :attr:`__annotations__`
47-
special attribute of modules, classes, and functions,
48-
respectively.
52+
can be retrieved by calling :func:`annotationlib.get_annotations`
53+
on modules, classes, and functions, respectively.
4954

50-
See :term:`variable annotation`, :term:`function annotation`, :pep:`484`
51-
and :pep:`526`, which describe this functionality.
55+
See :term:`variable annotation`, :term:`function annotation`, :pep:`484`,
56+
:pep:`526`, and :pep:`649`, which describe this functionality.
5257
Also see :ref:`annotations-howto`
5358
for best practices on working with annotations.
5459

@@ -366,6 +371,11 @@ Glossary
366371
statements. The technique contrasts with the :term:`LBYL` style
367372
common to many other languages such as C.
368373

374+
evaluate function
375+
A function that can be called to evaluate a lazily evaluated attribute
376+
of an object, such as the value of type aliases created with the :keyword:`type`
377+
statement.
378+
369379
expression
370380
A piece of syntax which can be evaluated to some value. In other words,
371381
an expression is an accumulation of expression elements like literals,

Doc/howto/annotations.rst

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,16 @@ Accessing The Annotations Dict Of An Object In Python 3.10 And Newer
3434

3535
Python 3.10 adds a new function to the standard library:
3636
:func:`inspect.get_annotations`. In Python versions 3.10
37-
and newer, calling this function is the best practice for
37+
through 3.13, calling this function is the best practice for
3838
accessing the annotations dict of any object that supports
3939
annotations. This function can also "un-stringize"
4040
stringized annotations for you.
4141

42+
In Python 3.14, there is a new :mod:`annotationlib` module
43+
with functionality for working with annotations. This
44+
includes a :func:`annotationlib.get_annotations` function,
45+
which supersedes :func:`inspect.get_annotations`.
46+
4247
If for some reason :func:`inspect.get_annotations` isn't
4348
viable for your use case, you may access the
4449
``__annotations__`` data member manually. Best practice
@@ -184,7 +189,11 @@ Best Practices For ``__annotations__`` In Any Python Version
184189
* If you do assign directly to the ``__annotations__`` member
185190
of an object, you should always set it to a ``dict`` object.
186191

187-
* If you directly access the ``__annotations__`` member
192+
* You should avoid accessing ``__annotations__`` directly on any object.
193+
Instead, use :func:`annotationlib.get_annotations` (Python 3.14+)
194+
or :func:`inspect.get_annotations` (Python 3.10+).
195+
196+
* If you do directly access the ``__annotations__`` member
188197
of an object, you should ensure that it's a
189198
dictionary before attempting to examine its contents.
190199

@@ -231,3 +240,11 @@ itself be quoted. In effect the annotation is quoted
231240

232241
This prints ``{'a': "'str'"}``. This shouldn't really be considered
233242
a "quirk"; it's mentioned here simply because it might be surprising.
243+
244+
If you use a class with a custom metaclass and access ``__annotations__``
245+
on the class, you may observe unexpected behavior; see
246+
:pep:`749 <749#pep749-metaclasses>` for some examples. You can avoid these
247+
quirks by using :func:`annotationlib.get_annotations` on Python 3.14+ or
248+
:func:`inspect.get_annotations` on Python 3.10+. On earlier versions of
249+
Python, you can avoid these bugs by accessing the annotations from the
250+
class's ``__dict__`` (e.g., ``cls.__dict__.get('__annotations__', None)``).

Doc/library/__future__.rst

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,10 @@ language using this mechanism:
6464
| generator_stop | 3.5.0b1 | 3.7 | :pep:`479`: |
6565
| | | | *StopIteration handling inside generators* |
6666
+------------------+-------------+--------------+---------------------------------------------+
67-
| annotations | 3.7.0b1 | TBD [1]_ | :pep:`563`: |
68-
| | | | *Postponed evaluation of annotations* |
67+
| annotations | 3.7.0b1 | Never [1]_ | :pep:`563`: |
68+
| | | | *Postponed evaluation of annotations*, |
69+
| | | | :pep:`649`: *Deferred evalutation of |
70+
| | | | annotations using descriptors* |
6971
+------------------+-------------+--------------+---------------------------------------------+
7072

7173
.. XXX Adding a new entry? Remember to update simple_stmts.rst, too.
@@ -115,11 +117,9 @@ language using this mechanism:
115117

116118
.. [1]
117119
``from __future__ import annotations`` was previously scheduled to
118-
become mandatory in Python 3.10, but the Python Steering Council
119-
twice decided to delay the change
120-
(`announcement for Python 3.10 <https://mail.python.org/archives/list/[email protected]/message/CLVXXPQ2T2LQ5MP2Y53VVQFCXYWQJHKZ/>`__;
121-
`announcement for Python 3.11 <https://mail.python.org/archives/list/[email protected]/message/VIZEBX5EYMSYIJNDBF6DMUMZOCWHARSO/>`__).
122-
No final decision has been made yet. See also :pep:`563` and :pep:`649`.
120+
become mandatory in Python 3.10, but the change was delayed and ultimately
121+
canceled. This feature will eventually be deprecated and removed. See
122+
:pep:`649` and :pep:`749`.
123123
124124
125125
.. seealso::

0 commit comments

Comments
 (0)