mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
[3.12] gh-97797: Improve documentation for typing.Annotated (GH-105365) (#105448)
gh-97797: Improve documentation for typing.Annotated (GH-105365)
(cherry picked from commit e26d296984
)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
parent
92ab560ef5
commit
241c36e1dc
1 changed files with 54 additions and 19 deletions
|
@ -5,6 +5,7 @@
|
||||||
.. testsetup:: *
|
.. testsetup:: *
|
||||||
|
|
||||||
import typing
|
import typing
|
||||||
|
from dataclasses import dataclass
|
||||||
from typing import *
|
from typing import *
|
||||||
|
|
||||||
.. module:: typing
|
.. module:: typing
|
||||||
|
@ -1184,7 +1185,8 @@ These can be used as types in annotations using ``[]``, each having a unique syn
|
||||||
(possibly multiple pieces of it, as ``Annotated`` is variadic).
|
(possibly multiple pieces of it, as ``Annotated`` is variadic).
|
||||||
Specifically, a type ``T`` can be annotated with metadata ``x`` via the
|
Specifically, a type ``T`` can be annotated with metadata ``x`` via the
|
||||||
typehint ``Annotated[T, x]``. This metadata can be used for either static
|
typehint ``Annotated[T, x]``. This metadata can be used for either static
|
||||||
analysis or at runtime. If a library (or tool) encounters a typehint
|
analysis or at runtime: at runtime, it is stored in a :attr:`__metadata__`
|
||||||
|
attribute. If a library (or tool) encounters a typehint
|
||||||
``Annotated[T, x]`` and has no special logic for metadata ``x``, it
|
``Annotated[T, x]`` and has no special logic for metadata ``x``, it
|
||||||
should ignore it and simply treat the type as ``T``. Unlike the
|
should ignore it and simply treat the type as ``T``. Unlike the
|
||||||
``no_type_check`` functionality that currently exists in the ``typing``
|
``no_type_check`` functionality that currently exists in the ``typing``
|
||||||
|
@ -1211,7 +1213,14 @@ These can be used as types in annotations using ``[]``, each having a unique syn
|
||||||
the same (or different) type(s) on any node, the tools or libraries
|
the same (or different) type(s) on any node, the tools or libraries
|
||||||
consuming those annotations are in charge of dealing with potential
|
consuming those annotations are in charge of dealing with potential
|
||||||
duplicates. For example, if you are doing value range analysis you might
|
duplicates. For example, if you are doing value range analysis you might
|
||||||
allow this::
|
allow this:
|
||||||
|
|
||||||
|
.. testcode::
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class ValueRange:
|
||||||
|
lo: int
|
||||||
|
hi: int
|
||||||
|
|
||||||
T1 = Annotated[int, ValueRange(-10, 5)]
|
T1 = Annotated[int, ValueRange(-10, 5)]
|
||||||
T2 = Annotated[T1, ValueRange(-20, 3)]
|
T2 = Annotated[T1, ValueRange(-20, 3)]
|
||||||
|
@ -1226,6 +1235,10 @@ These can be used as types in annotations using ``[]``, each having a unique syn
|
||||||
* Multiple type annotations are supported (``Annotated`` supports variadic
|
* Multiple type annotations are supported (``Annotated`` supports variadic
|
||||||
arguments)::
|
arguments)::
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class ctype:
|
||||||
|
kind: str
|
||||||
|
|
||||||
Annotated[int, ValueRange(3, 10), ctype("char")]
|
Annotated[int, ValueRange(3, 10), ctype("char")]
|
||||||
|
|
||||||
* ``Annotated`` must be called with at least two arguments (
|
* ``Annotated`` must be called with at least two arguments (
|
||||||
|
@ -1234,30 +1247,52 @@ These can be used as types in annotations using ``[]``, each having a unique syn
|
||||||
* The order of the annotations is preserved and matters for equality
|
* The order of the annotations is preserved and matters for equality
|
||||||
checks::
|
checks::
|
||||||
|
|
||||||
Annotated[int, ValueRange(3, 10), ctype("char")] != Annotated[
|
assert Annotated[int, ValueRange(3, 10), ctype("char")] != Annotated[
|
||||||
int, ctype("char"), ValueRange(3, 10)
|
int, ctype("char"), ValueRange(3, 10)
|
||||||
]
|
]
|
||||||
|
|
||||||
* Nested ``Annotated`` types are flattened, with metadata ordered
|
* Nested ``Annotated`` types are flattened, with metadata ordered
|
||||||
starting with the innermost annotation::
|
starting with the innermost annotation::
|
||||||
|
|
||||||
Annotated[Annotated[int, ValueRange(3, 10)], ctype("char")] == Annotated[
|
assert Annotated[Annotated[int, ValueRange(3, 10)], ctype("char")] == Annotated[
|
||||||
int, ValueRange(3, 10), ctype("char")
|
int, ValueRange(3, 10), ctype("char")
|
||||||
]
|
]
|
||||||
|
|
||||||
* Duplicated annotations are not removed::
|
* Duplicated annotations are not removed::
|
||||||
|
|
||||||
Annotated[int, ValueRange(3, 10)] != Annotated[
|
assert Annotated[int, ValueRange(3, 10)] != Annotated[
|
||||||
int, ValueRange(3, 10), ValueRange(3, 10)
|
int, ValueRange(3, 10), ValueRange(3, 10)
|
||||||
]
|
]
|
||||||
|
|
||||||
* ``Annotated`` can be used with nested and generic aliases::
|
* ``Annotated`` can be used with nested and generic aliases:
|
||||||
|
|
||||||
T = TypeVar('T')
|
.. testcode::
|
||||||
Vec = Annotated[list[tuple[T, T]], MaxLen(10)]
|
|
||||||
V = Vec[int]
|
|
||||||
|
|
||||||
V == Annotated[list[tuple[int, int]], MaxLen(10)]
|
@dataclass
|
||||||
|
class MaxLen:
|
||||||
|
value: int
|
||||||
|
|
||||||
|
type Vec[T] = Annotated[list[tuple[T, T]], MaxLen(10)]
|
||||||
|
|
||||||
|
# When used in a type annotation, a type checker will treat "V" the same as
|
||||||
|
# ``Annotated[list[tuple[int, int]], MaxLen(10)]``:
|
||||||
|
type V = Vec[int]
|
||||||
|
|
||||||
|
.. attribute:: __metadata__
|
||||||
|
|
||||||
|
At runtime, the metadata associated with an ``Annotated`` type can be
|
||||||
|
retrieved via the ``__metadata__`` attribute.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
.. doctest::
|
||||||
|
|
||||||
|
>>> from typing import Annotated
|
||||||
|
>>> X = Annotated[int, "very", "important", "metadata"]
|
||||||
|
>>> X
|
||||||
|
typing.Annotated[int, 'very', 'important', 'metadata']
|
||||||
|
>>> X.__metadata__
|
||||||
|
('very', 'important', 'metadata')
|
||||||
|
|
||||||
.. versionadded:: 3.9
|
.. versionadded:: 3.9
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue