@@ -1315,47 +1315,37 @@ These can be used as types in annotations. They all support subscription using
1315
1315
T1 = Annotated[int, ValueRange(-10, 5)]
1316
1316
T2 = Annotated[T1, ValueRange(-20, 3)]
1317
1317
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::
1319
1321
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
<
10000
/code>
1323
+ class ctype:
1324
+ kind: str
1334
1325
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)]
1337
1328
1338
- * The order of the metadata elements is preserved and matters for equality
1339
- checks::
1329
+ assert a1 != a2 # Order matters
1340
1330
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.
1344
1334
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::
1347
1337
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
+ ]
1351
1341
1352
- * Duplicated metadata elements are not removed::
1342
+ Duplicated metadata elements are not removed::
1353
1343
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
+ ]
1357
1347
1358
- * ``Annotated `` can be used with nested and generic aliases:
1348
+ ``Annotated `` can be used with nested and generic aliases:
1359
1349
1360
1350
.. testcode ::
1361
1351
@@ -1369,19 +1359,15 @@ These can be used as types in annotations. They all support subscription using
1369
1359
# ``Annotated[list[tuple[int, int]], MaxLen(10)] ``:
1370
1360
type V = Vec[int]
1371
1361
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 `::
1377
1363
1378
- Annotated[T1, T2, T3, ..., Ann1]
1364
+ type Variadic[*Ts] = Annotated[*Ts, Ann1] = Annotated[ T1, T2, T3, ..., Ann1] # NOT valid
1379
1365
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.
1382
1368
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 ` strips the metadata from annotations.
1370
+ Pass ``include_extras=True `` to have the metadata preserved:
1385
1371
1386
1372
.. doctest ::
1387
1373
@@ -1393,8 +1379,8 @@ These can be used as types in annotations. They all support subscription using
1393
1379
>>> get_type_hints(func, include_extras = True )
1394
1380
{'x': typing.Annotated[int, 'metadata'], 'return': <class 'NoneType'>}
1395
1381
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:
1398
1384
1399
1385
.. doctest ::
1400
1386
@@ -1405,8 +1391,8 @@ These can be used as types in annotations. They all support subscription using
1405
1391
>>> X.__metadata__
1406
1392
('very', 'important', 'metadata')
1407
1393
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:
1410
1396
1411
1397
.. doctest ::
1412
1398
@@ -1415,7 +1401,7 @@ These can be used as types in annotations. They all support subscription using
1415
1401
>>> Password.__origin__
1416
1402
<class 'str'>
1417
1403
1418
- Note that using :func: `get_origin ` will return ``Annotated `` itself:
1404
+ Note that using :func: `get_origin ` will return ``Annotated `` itself:
1419
1405
1420
1406
.. doctest ::
1421
1407
0 commit comments