8000 [3.13] gh-124269: Simplify `typing.Annotated` docs (GH-130770) (#131222) · python/cpython@0f97abf · GitHub
[go: up one dir, main page]

Skip to content

Commit 0f97abf

Browse files
miss-islingtonStanFromIrelandJelleZijlstrapicnixz
authored
[3.13] gh-124269: Simplify typing.Annotated docs (GH-130770) (#131222)
gh-124269: Simplify `typing.Annotated` docs (GH-130770) --------- (cherry picked from commit e4ac196) Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com> Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com> Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
1 parent 78f75d3 commit 0f97abf

File tree

1 file changed

+33
-47
lines changed

1 file changed

+33
-47
lines changed

Doc/library/typing.rst

Lines changed: 33 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,47 +1368,37 @@ 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+
The first argument to ``Annotated`` must be a valid type. Multiple metadata
1372+
elements can be supplied as ``Annotated`` supports variadic arguments. The
1373+
order of the metadata elements is preserved and matters for equality checks::
13721374

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.
1375+
@dataclass
1376+
class ctype:
1377+
kind: str
13871378

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

1391-
* The order of the metadata elements is preserved and matters for equality
1392-
checks::
1382+
assert a1 != a2 # Order matters
13931383

1394-
assert Annotated[int, ValueRange(3, 10), ctype("char")] != Annotated[
1395-
int, ctype("char"), ValueRange(3, 10)
1396-
]
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.
13971387

1398-
* Nested ``Annotated`` types are flattened. The order of the metadata elements
1399-
starts with the innermost annotation::
1388+
Nested ``Annotated`` types are flattened. The order of the metadata elements
1389+
starts with the innermost annotation::
14001390

1401-
assert Annotated[Annotated[int, ValueRange(3, 10)], ctype("char")] == Annotated[
1402-
int, ValueRange(3, 10), ctype("char")
1403-
]
1391+
assert Annotated[Annotated[int, ValueRange(3, 10)], ctype("char")] == Annotated[
1392+
int, ValueRange(3, 10), ctype("char")
1393+
]
14041394

1405-
* Duplicated metadata elements are not removed::
1395+
Duplicated metadata elements are not removed::
14061396

1407-
assert Annotated[int, ValueRange(3, 10)] != Annotated[
1408-
int, ValueRange(3, 10), ValueRange(3, 10)
1409-
]
1397+
assert Annotated[int, ValueRange(3, 10)] != Annotated[
1398+
int, ValueRange(3, 10), ValueRange(3, 10)
1399+
]
14101400

1411-
* ``Annotated`` can be used with nested and generic aliases:
1401+
``Annotated`` can be used with nested and generic aliases:
14121402

14131403
.. testcode::
14141404

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

1425-
* ``Annotated`` cannot be used with an unpacked :class:`TypeVarTuple`::
1426-
1427-
type Variadic[*Ts] = Annotated[*Ts, Ann1] # NOT valid
1428-
1429-
This would be equivalent to::
1415+
``Annotated`` cannot be used with an unpacked :class:`TypeVarTuple`::
14301416

1431-
Annotated[T1, T2, T3, ..., Ann1]
1417+
type Variadic[*Ts] = Annotated[*Ts, Ann1] = Annotated[T1, T2, T3, ..., Ann1] # NOT valid
14321418

1433-
where ``T1``, ``T2``, etc. are :class:`TypeVars <TypeVar>`. This would be
1434-
invalid: only one type should be passed to Annotated.
1419+
where ``T1``, ``T2``, ... are :class:`TypeVars <TypeVar>`. This is invalid as
1420+
only one type should be passed to Annotated.
14351421

1436-
* By default, :func:`get_type_hints` strips the metadata from annotations.
1437-
Pass ``include_extras=True`` to have the metadata preserved:
1422+
By default, :func:`get_type_hints` strips the metadata from annotations.
1423+
Pass ``include_extras=True`` to have the metadata preserved:
14381424

14391425
.. doctest::
14401426

@@ -1446,8 +1432,8 @@ These can be used as types in annotations. They all support subscription using
14461432
>>> get_type_hints(func, include_extras=True)
14471433
{'x': typing.Annotated[int, 'metadata'], 'return': <class 'NoneType'>}
14481434

1449-
* At runtime, the metadata associated with an ``Annotated`` type can be
1450-
retrieved via the :attr:`!__metadata__` attribute:
1435+
At runtime, the metadata associated with an ``Annotated`` type can be
1436+
retrieved via the :attr:`!__metadata__` attribute:
14511437

14521438
.. doctest::
14531439

@@ -1458,8 +1444,8 @@ These can be used as types in annotations. They all support subscription using
14581444
>>> X.__metadata__
14591445
('very', 'important', 'metadata')
14601446

1461-
* At runtime, if you want to retrieve the original
1462-
type wrapped by ``Annotated``, use the :attr:`!__origin__` attribute:
1447+
If you want to retrieve the original type wrapped by ``Annotated``, use the
1448+
:attr:`!__origin__` attribute:
14631449

14641450
.. doctest::
14651451

@@ -1468,7 +1454,7 @@ These can be used as types in annotations. They all support subscription using
14681454
>>> Password.__origin__
14691455
<class 'str'>
14701456

1471-
Note that using :func:`get_origin` will return ``Annotated`` itself:
1457+
Note that using :func:`get_origin` will return ``Annotated`` itself:
14721458

14731459
.. doctest::
14741460

0 commit comments

Comments
 (0)
0