8000 [3.12] gh-124269: Simplify `typing.Annotated` docs (GH-130770) (#131223) · python/cpython@39b4f8b · GitHub
[go: up one dir, main page]

Skip to content

Commit 39b4f8b

Browse files
miss-islingtonStanFromIrelandJelleZijlstrapicnixz
authored
[3.12] gh-124269: Simplify typing.Annotated docs (GH-130770) (#131223)
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 a3f4889 commit 39b4f8b

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
@@ -1315,47 +1315,37 @@ These can be used as types in annotations. They all support subscription using
13151315
T1 = Annotated[int, ValueRange(-10, 5)]
13161316
T2 = Annotated[T1, ValueRange(-20, 3)]
13171317

1318-
Details of the syntax:
1318+
The first argument to ``Annotated`` must be a valid type. Multiple metadata
1319+
elements can be supplied as ``Annotated`` supports variadic arguments. The
1320+
order of the metadata elements is preserved and matters for equality checks::
13191321

1320-
* The first argument to ``Annotated`` must be a valid type
1321-
1322-
* Multiple metadata elements can be supplied (``Annotated`` supports variadic
1323-
arguments)::
1324-
1325-
@dataclass
1326-
class ctype:
1327-
kind: str
1328-
1329-
Annotated[int, ValueRange(3, 10), ctype("char")]
1330-
1331-
It is up to the tool consuming the annotations to decide whether the
1332-
client is allowed to add multiple metadata elements to one annotation and how to
1333-
merge those annotations.
1322+
@dataclass
1323+
class ctype:
1324+
kind: str
13341325

1335-
* ``Annotated`` must be subscripted with at least two arguments (
1336-
``Annotated[int]`` is not valid)
1326+
a1 = Annotated[int, ValueRange(3, 10), ctype("char")]
1327+
a2 = Annotated[int, ctype("char"), ValueRange(3, 10)]
13371328

1338-
* The order of the metadata elements is preserved and matters for equality
1339-
checks::
1329+
assert a1 != a2 # Order matters
13401330

1341-
assert Annotated[int, ValueRange(3, 10), ctype("char")] != Annotated[
1342-
int, ctype("char"), ValueRange(3, 10)
1343-
]
1331+
It is up to the tool consuming the annotations to decide whether the
1332+
client is allowed to add multiple metadata elements to one annotation and how to
1333+
merge those annotations.
13441334

1345-
* Nested ``Annotated`` types are flattened. The order of the metadata elements
1346-
starts with the innermost annotation::
1335+
Nested ``Annotated`` types are flattened. The order of the metadata elements
1336+
starts with the innermost annotation::
13471337

1348-
assert Annotated[Annotated[int, ValueRange(3, 10)], ctype("char")] == Annotated[
1349-
int, ValueRange(3, 10), ctype("char")
1350-
]
1338+
assert Annotated[Annotated[int, ValueRange(3, 10)], ctype("char")] == Annotated[
1339+
int, ValueRange(3, 10), ctype("char")
1340+
]
13511341

1352-
* Duplicated metadata elements are not removed::
1342+
Duplicated metadata elements are not removed::
13531343

1354-
assert Annotated[int, ValueRange(3, 10)] != Annotated[
1355-
int, ValueRange(3, 10), ValueRange(3, 10)
1356-
]
1344+
assert Annotated[int, ValueRange(3, 10)] != Annotated[
1345+
int, ValueRange(3, 10), ValueRange(3, 10)
1346+
]
13571347

1358-
* ``Annotated`` can be used with nested and generic aliases:
1348+
``Annotated`` can be used with nested and generic aliases:
13591349

13601350
.. testcode::
13611351

@@ -1369,19 +1359,15 @@ These can be used as types in annotations. They all support subscription using
13691359
# ``Annotated[list[tuple[int, int]], MaxLen(10)]``:
13701360
type V = Vec[int]
13711361

1372-
* ``Annotated`` cannot be used with an unpacked :class:`TypeVarTuple`::
1373-
1374-
type Variadic[*Ts] = Annotated[*Ts, Ann1] # NOT valid
1375-
1376-
This would be equivalent to::
1362+
``Annotated`` cannot be used with an unpacked :class:`TypeVarTuple`::
13771363

1378-
Annotated[T1, T2, T3, ..., Ann1]
1364+
type Variadic[*Ts] = Annotated[*Ts, Ann1] = Annotated[T1, T2, T3, ..., Ann1] # NOT valid
13791365

1380-
where ``T1``, ``T2``, etc. are :class:`TypeVars <TypeVar>`. This would be
1381-
invalid: only one type should be passed to Annotated.
1366+
where ``T1``, ``T2``, ... are :class:`TypeVars <TypeVar>`. This is invalid as
1367+
only one type should be passed to Annotated.
13821368

1383-
* By default, :func:`get_type_hints` strips the metadata from annotations.
1384-
Pass ``include_extras=True`` to have the metadata preserved:
1369+
By default, :func:`get_type_hints` strip 6DB6 s the metadata from annotations.
1370+
Pass ``include_extras=True`` to have the metadata preserved:
13851371

13861372
.. doctest::
13871373

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

1396-
* At runtime, the metadata associated with an ``Annotated`` type can be
1397-
retrieved via the :attr:`!__metadata__` attribute:
1382+
At runtime, the metadata associated with an ``Annotated`` type can be
1383+
retrieved via the :attr:`!__metadata__` attribute:
13981384

13991385
.. doctest::
14001386

@@ -1405,8 +1391,8 @@ These can be used as types in annotations. They all support subscription using
14051391
>>> X.__metadata__
14061392
('very', 'important', 'metadata')
14071393

1408-
* At runtime, if you want to retrieve the original
1409-
type wrapped by ``Annotated``, use the :attr:`!__origin__` attribute:
1394+
If you want to retrieve the original type wrapped by ``Annotated``, use the
1395+
:attr:`!__origin__` attribute:
14101396

14111397
.. doctest::
14121398

@@ -1415,7 +1401,7 @@ These can be used as types in annotations. They all support subscription using
14151401
>>> Password.__origin__
14161402
<class 'str'>
14171403

1418-
Note that using :func:`get_origin` will return ``Annotated`` itself:
1404+
Note that using :func:`get_origin` will return ``Annotated`` itself:
14191405

14201406
.. doctest::
14211407

0 commit comments

Comments
 (0)
0