-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
gh-119180: Documentation for PEP 649 and 749 #122235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
20dcb62
206560c
edd12a2
79ce456
3f70c21
e297349
d52fb08
4e7a2a5
35312e7
b30abbd
bcb6a9f
c82fb66
b4f6385
6ebdce9
52dc99e
61eedbd
27668ce
6254e18
c42cb60
1873912
86001c1
c1943fa
889f5aa
6dd39fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,8 +64,10 @@ language using this mechanism: | |
| generator_stop | 3.5.0b1 | 3.7 | :pep:`479`: | | ||
| | | | *StopIteration handling inside generators* | | ||
+------------------+-------------+--------------+---------------------------------------------+ | ||
| annotations | 3.7.0b1 | TBD [1]_ | :pep:`563`: | | ||
| | | | *Postponed evaluation of annotations* | | ||
| annotations | 3.7.0b1 | Never [1]_ | :pep:`563`: | | ||
| | | | *Postponed evaluation of annotations*, | | ||
| | | | :pep:`649`: *Deferred evalutation of | | ||
| | | | annotations using descriptors* | | ||
+------------------+-------------+--------------+---------------------------------------------+ | ||
|
||
.. XXX Adding a new entry? Remember to update simple_stmts.rst, too. | ||
|
@@ -115,11 +117,9 @@ language using this mechanism: | |
|
||
.. [1] | ||
``from __future__ import annotations`` was previously scheduled to | ||
become mandatory in Python 3.10, but the Python Steering Council | ||
twice decided to delay the change | ||
(`announcement for Python 3.10 <https://mail.python.org/archives/list/[email protected]/message/CLVXXPQ2T2LQ5MP2Y53VVQFCXYWQJHKZ/>`__; | ||
`announcement for Python 3.11 <https://mail.python.org/archives/list/[email protected]/message/VIZEBX5EYMSYIJNDBF6DMUMZOCWHARSO/>`__). | ||
No final decision has been made yet. See also :pep:`563` and :pep:`649`. | ||
become mandatory in Python 3.10, but the change was delayed and ultimately | ||
canceled. This feature will eventually be deprecated and removed. See | ||
:pep:`649` and :pep:`749`. | ||
Comment on lines
119
to
+122
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we provide or link to migration guidance here, especially given that this is the first time a |
||
|
||
|
||
.. seealso:: | ||
|
JelleZijlstra marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,178 @@ | ||||||||||||||||
:mod:`!annotationlib` --- Functionality for introspecting annotations | ||||||||||||||||
===================================================================== | ||||||||||||||||
|
||||||||||||||||
.. module:: annotationlib | ||||||||||||||||
:synopsis: Functionality for introspecting annotations | ||||||||||||||||
|
||||||||||||||||
|
||||||||||||||||
**Source code:** :source:`Lib/annotationlib.py` | ||||||||||||||||
|
||||||||||||||||
.. testsetup:: default | ||||||||||||||||
|
||||||||||||||||
import annotationlib | ||||||||||||||||
from annotationlib import * | ||||||||||||||||
|
||||||||||||||||
-------------- | ||||||||||||||||
|
||||||||||||||||
The :mod:`!annotationlib` module provides tools for introspecting :term:`annotations <annotation>` | ||||||||||||||||
on modules, classes, and functions. | ||||||||||||||||
|
||||||||||||||||
|
||||||||||||||||
.. function:: call_annotate_function(annotate, format, *, owner=None) | ||||||||||||||||
|
||||||||||||||||
Call the :term:`annotate function` *annotate* with the given *format*, a member of the :class:`Format` | ||||||||||||||||
enum, and return the annotations dictionary produced by the function. | ||||||||||||||||
|
||||||||||||||||
Annotate functions generated by the compiler for functions, classes, and modules support only | ||||||||||||||||
the :attr:`~Format.VALUE` format when called directly. This function calls the annotate function | ||||||||||||||||
in a special environment that allows it to produce annotations in the other formats. | ||||||||||||||||
AA-Turner marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||
|
||||||||||||||||
*owner* is the object that owns the annotation function, usually a function, class, or module. | ||||||||||||||||
If provided, it is used in the :attr:`~Format.FORWARDREF` format to produce :class:`ForwardRef` | ||||||||||||||||
JelleZijlstra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||
object that carry more information. | ||||||||||||||||
|
||||||||||||||||
.. versionadded:: 3.14 | ||||||||||||||||
|
||||||||||||||||
.. class:: Format | ||||||||||||||||
|
||||||||||||||||
A :class:`enum.IntEnum` describing the formats in which annotations can be returned. | ||||||||||||||||
JelleZijlstra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||
Members of the enum, or their equivalent integer values, can be passed to | ||||||||||||||||
:func:`get_annotations` and other functions in this module, as well as to | ||||||||||||||||
:attr:`~object.__annotate__` functions. | ||||||||||||||||
|
||||||||||||||||
.. attribute:: VALUE | ||||||||||||||||
|
||||||||||||||||
JelleZijlstra marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
Values are the result of evaluating the annotation expressions. | ||||||||||||||||
|
||||||||||||||||
This enum member's value is equal to 1. | ||||||||||||||||
|
||||||||||||||||
.. attribute:: FORWARDREF | ||||||||||||||||
|
||||||||||||||||
Values are real annotation values (as per :attr:`Format.VALUE` format) for defined values, | ||||||||||||||||
and :class:`ForwardRef` proxies for undefined values. Real objects may be exposed to, | ||||||||||||||||
or contain references to, :class:`ForwardRef` proxy objects. | ||||||||||||||||
JelleZijlstra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||
|
||||||||||||||||
This enum member's value is equal to 2. | ||||||||||||||||
|
||||||||||||||||
.. attribute:: SOURCE | ||||||||||||||||
|
||||||||||||||||
Values are the text string of the annotation as it appears in the source code. | ||||||||||||||||
May only be approximate; whitespace may be normalized, and constant values | ||||||||||||||||
may be optimized. It is possible the exact values of these strings could | ||||||||||||||||
change in future version of Python. | ||||||||||||||||
JelleZijlstra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||
|
||||||||||||||||
.. versionadded:: 3.14 | ||||||||||||||||
|
||||||||||||||||
.. class:: ForwardRef | ||||||||||||||||
|
||||||||||||||||
A proxy object for forward references in annotations. | ||||||||||||||||
|
||||||||||||||||
Instances of this class are returned when the :attr:`~Format.FORWARDREF` format is | ||||||||||||||||
used and annotations contain a name that cannot be resolved. This can happen | ||||||||||||||||
JelleZijlstra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||
when a forward reference is used in an annotation, such as when a class is | ||||||||||||||||
referenced before it is defined. | ||||||||||||||||
|
||||||||||||||||
.. attribute:: __forward_arg__ | ||||||||||||||||
|
||||||||||||||||
A string containing the code that was evaluated to produce the :class:`~ForwardRef`. | ||||||||||||||||
The string may not be exactly equivalent to the original source. | ||||||||||||||||
|
||||||||||||||||
.. method:: evaluate(*, globals=None, locals=None, type_params=None, owner=None) | ||||||||||||||||
|
||||||||||||||||
Evaluate the forward reference, returning its value. | ||||||||||||||||
|
||||||||||||||||
This may throw an exception such as :exc:`NameError` if the forward reference | ||||||||||||||||
JelleZijlstra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||
refers to names that do not exist. The parameters to the function can be used to | ||||||||||||||||
provide bindings for names that would otherwise be undefined. | ||||||||||||||||
|
||||||||||||||||
*globals* and *locals* are passed to :func:`eval()`, representing the global and | ||||||||||||||||
local namespaces in which the name is evaluated. *type_params*, if given, must be | ||||||||||||||||
a tuple of :ref:`type parameters <type-params>` that are in scope while the forward | ||||||||||||||||
reference is being evaluated. *owner* is the object that owns the annotation from | ||||||||||||||||
which the forward reference derives, usually a function, class, or module. | ||||||||||||||||
:class:`~ForwardRef` instances returned by :func:`get_annotations` retain | ||||||||||||||||
JelleZijlstra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||
a reference to their owner, so it is not necessary to pass it in explicitly. | ||||||||||||||||
|
||||||||||||||||
Once a :class:`~ForwardRef` instance has been evaluated, it caches the evaluated | ||||||||||||||||
value, and future calls to :meth:`evaluate` will return the cached value, regardless | ||||||||||||||||
of the parameters passed in. | ||||||||||||||||
JelleZijlstra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||
|
||||||||||||||||
.. versionadded:: 3.14 | ||||||||||||||||
|
||||||||||||||||
.. function:: get_annotate_function(obj) | ||||||||||||||||
|
||||||||||||||||
Retrieve the :term:`annotate function` for *obj*. Return ``None`` if *obj* does not have an | ||||||||||||||||
annotate function. | ||||||||||||||||
|
||||||||||||||||
This is usually equivalent to accessing the :attr:`~object.__annotate__` attribute of *obj*, | ||||||||||||||||
but direct access to the attribute may return the wrong object in certain situations involving | ||||||||||||||||
metaclasses. It is recommended to use this function instead of accessing the attribute directly. | ||||||||||||||||
JelleZijlstra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||
|
||||||||||||||||
.. versionadded:: 3.14 | ||||||||||||||||
|
||||||||||||||||
.. function:: get_annotations(obj, *, globals=None, locals=None, eval_str=False, format=Format.VALUE) | ||||||||||||||||
|
||||||||||||||||
Compute the annotations dict for an object. | ||||||||||||||||
JelleZijlstra marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
|
||||||||||||||||
``obj`` may be a callable, class, module, or other object with | ||||||||||||||||
JelleZijlstra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||
:attr:`~object.__annotate__` and :attr:`~object.__annotations__` attributes. | ||||||||||||||||
Passing in an object of any other type raises :exc:`TypeError`. | ||||||||||||||||
|
||||||||||||||||
The *format* parameter controls the format in which annotations are returned: | ||||||||||||||||
JelleZijlstra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||
|
||||||||||||||||
Returns a dict. ``get_annotations()`` returns a new dict every time | ||||||||||||||||
it's called; calling it twice on the same object will return two | ||||||||||||||||
different but equivalent dicts. | ||||||||||||||||
|
Returns a dict. ``get_annotations()`` returns a new dict every time | |
it's called; calling it twice on the same object will return two | |
different but equivalent dicts. | |
.. note:: | |
This function returns a *new* dict every time it's called; calling it | |
twice on the same object returns two different but equivalent dicts. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why should this be indented into a note? I feel that makes the text noisier.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'll have a nice box around this (important) behaviour. Since your bullet list contained "Always, always, always creates a new dict", I feel this behaviour should be highlighted. I used "note" but you can also use "important" or "tip".
JelleZijlstra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
JelleZijlstra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
JelleZijlstra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
JelleZijlstra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
JelleZijlstra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
JelleZijlstra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
JelleZijlstra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
picnixz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
JelleZijlstra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,4 +25,5 @@ overview: | |
__future__.rst | ||
gc.rst | ||
inspect.rst | ||
annotationlib.rst | ||
site.rst |
Uh oh!
There was an error while loading. Please reload this page.