Skip to content

Commit b3e2c90

Browse files
Simplify Annotated docs
1 parent a42168d commit b3e2c90

File tree

1 file changed

+35
-53
lines changed

1 file changed

+35
-53
lines changed

Doc/library/typing.rst

Lines changed: 35 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,47 +1368,39 @@ These can be used as types in annotations. They all support subscription using
13681368
T1 = Annotated[int, ValueRange(-10, 5)]
13691369
T2 = Annotated[T1, ValueRange(-20, 3)]
13701370

1371-
Details of the syntax:
1371+
``Annotated`` must be subscripted with at least two arguments.
1372+
The first argument to ``Annotated`` must be a valid type. Multiple metadata
1373+
elements can be supplied as ``Annotated`` supports variadic arguments. The
1374+
order of the metadata elements is preserved and matters for equality checks::
13721375

1373-
* The first argument to ``Annotated`` must be a valid type
1374-
1375-
* Multiple metadata elements can be supplied (``Annotated`` supports variadic
1376-
arguments)::
1377-
1378-
@dataclass
1379-
class ctype:
1380-
kind: str
1381-
1382-
Annotated[int, ValueRange(3, 10), ctype("char")]
1383-
1384-
It is up to the tool consuming the annotations to decide whether the
1385-
client is allowed to add multiple metadata elements to one annotation and how to
1386-
merge those annotations.
1376+
@dataclass
1377+
class ctype:
1378+
kind: str
13871379

1388-
* ``Annotated`` must be subscripted with at least two arguments (
1389-
``Annotated[int]`` is not valid)
1380+
Annotated[int, ValueRange(3, 10), ctype("char")]
13901381

1391-
* The order of the metadata elements is preserved and matters for equality
1392-
checks::
1382+
assert Annotated[int, ValueRange(3, 10), ctype("char")] != Annotated[
1383+
int, ctype("char"), ValueRange(3, 10)
1384+
] # Order matters
13931385

1394-
assert Annotated[int, ValueRange(3, 10), ctype("char")] != Annotated[
1395-
int, ctype("char"), ValueRange(3, 10)
1396-
]
1386+
It is up to the tool consuming the annotations to decide whether the
1387+
client is allowed to add multiple metadata elements to one annotation and how to
1388+
merge those annotations.
13971389

1398-
* Nested ``Annotated`` types are flattened. The order of the metadata elements
1399-
starts with the innermost annotation::
1390+
Nested ``Annotated`` types are flattened. The order of the metadata elements
1391+
starts with the innermost annotation::
14001392

1401-
assert Annotated[Annotated[int, ValueRange(3, 10)], ctype("char")] == Annotated[
1402-
int, ValueRange(3, 10), ctype("char")
1403-
]
1393+
assert Annotated[Annotated[int, ValueRange(3, 10)], ctype("char")] == Annotated[
1394+
int, ValueRange(3, 10), ctype("char")
1395+
]
14041396

1405-
* Duplicated metadata elements are not removed::
1397+
Duplicated metadata elements are not removed::
14061398

1407-
assert Annotated[int, ValueRange(3, 10)] != Annotated[
1408-
int, ValueRange(3, 10), ValueRange(3, 10)
1409-
]
1399+
assert Annotated[int, ValueRange(3, 10)] != Annotated[
1400+
int, ValueRange(3, 10), ValueRange(3, 10)
1401+
]
14101402

1411-
* ``Annotated`` can be used with nested and generic aliases:
1403+
``Annotated`` can be used with nested and generic aliases:
14121404

14131405
.. testcode::
14141406

@@ -1422,19 +1414,15 @@ These can be used as types in annotations. They all support subscription using
14221414
# ``Annotated[list[tuple[int, int]], MaxLen(10)]``:
14231415
type V = Vec[int]
14241416

1425-
* ``Annotated`` cannot be used with an unpacked :class:`TypeVarTuple`::
1426-
1427-
type Variadic[*Ts] = Annotated[*Ts, Ann1] # NOT valid
1417+
``Annotated`` cannot be used with an unpacked :class:`TypeVarTuple`::
14281418

1429-
This would be equivalent to::
1419+
type Variadic[*Ts] = Annotated[*Ts, Ann1] = Annotated[T1, T2, T3, ..., Ann1] # NOT valid
14301420

1431-
Annotated[T1, T2, T3, ..., Ann1]
1421+
where ``T1``, ``T2``,... are :class:`TypeVars <TypeVar>`. This is invalid as
1422+
only one type should be passed to Annotated.
14321423

1433-
where ``T1``, ``T2``, etc. are :class:`TypeVars <TypeVar>`. This would be
1434-
invalid: only one type should be passed to Annotated.
1435-
1436-
* By default, :func:`get_type_hints` strips the metadata from annotations.
1437-
Pass ``include_extras=True`` to have the metadata preserved:
1424+
By default, :func:`get_type_hints` strips the metadata from annotations.
1425+
Pass ``include_extras=True`` to have the metadata preserved:
14381426

14391427
.. doctest::
14401428

@@ -1446,29 +1434,23 @@ These can be used as types in annotations. They all support subscription using
14461434
>>> get_type_hints(func, include_extras=True)
14471435
{'x': typing.Annotated[int, 'metadata'], 'return': <class 'NoneType'>}
14481436

1449-
* At runtime, the metadata associated with an ``Annotated`` type can be
1450-
retrieved via the :attr:`!__metadata__` attribute:
1437+
At runtime, the metadata associated with an ``Annotated`` type can be
1438+
retrieved via the :attr:`!__metadata__` attribute. If you want to retrieve
1439+
the original type wrapped by ``Annotated``, use the :attr:`!__origin__` attribute:
14511440

14521441
.. doctest::
14531442

1454-
>>> from typing import Annotated
1443+
>>> from typing import Annotated, get_origin
14551444
>>> X = Annotated[int, "very", "important", "metadata"]
14561445
>>> X
14571446
typing.Annotated[int, 'very', 'important', 'metadata']
14581447
>>> X.__metadata__
14591448
('very', 'important', 'metadata')
1460-
1461-
* At runtime, if you want to retrieve the original
1462-
type wrapped by ``Annotated``, use the :attr:`!__origin__` attribute:
1463-
1464-
.. doctest::
1465-
1466-
>>> from typing import Annotated, get_origin
14671449
>>> Password = Annotated[str, "secret"]
14681450
>>> Password.__origin__
14691451
<class 'str'>
14701452

1471-
Note that using :func:`get_origin` will return ``Annotated`` itself:
1453+
Note that using :func:`get_origin` will return ``Annotated`` itself:
14721454

14731455
.. doctest::
14741456

0 commit comments

Comments
 (0)