@@ -1368,47 +1368,37 @@ These can be used as types in annotations. They all support subscription using
1368
1368
T1 = Annotated[int, ValueRange(-10, 5)]
1369
1369
T2 = Annotated[T1, ValueRange(-20, 3)]
1370
1370
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::
1372
1374
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
1387
1378
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)]
1390
1381
1391
- * The order of the metadata elements is preserved and matters for equality
1392
- checks::
1382
+ assert a1 != a2 # Order matters
1393
1383
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.
1397
1387
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::
1400
1390
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
+ ]
1404
1394
1405
- * Duplicated metadata elements are not removed::
1395
+ Duplicated metadata elements are not removed::
1406
1396
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
+ ]
1410
1400
1411
- * ``Annotated `` can be used with nested and generic aliases:
1401
+ ``Annotated `` can be used with nested and generic aliases:
1412
1402
1413
1403
.. testcode ::
1414
1404
@@ -1422,19 +1412,15 @@ These can be used as types in annotations. They all support subscription using
1422
1412
# ``Annotated[list[tuple[int, int]], MaxLen(10)] ``:
1423
1413
type V = Vec[int]
1424
1414
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 `::
1430
1416
1431
- Annotated[T1, T2, T3, ..., Ann1]
1417
+ type Variadic[*Ts] = Annotated[*Ts, Ann1] = Annotated[ T1, T2, T3, ..., Ann1] # NOT valid
1432
1418
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.
1435
1421
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:
1438
1424
1439
1425
.. doctest ::
1440
1426
@@ -1446,8 +1432,8 @@ These can be used as types in annotations. They all support subscription using
1446
1432
>>> get_type_hints(func, include_extras = True )
1447
1433
{'x': typing.Annotated[int, 'metadata'], 'return': <class 'NoneType'>}
1448
1434
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:
1451
1437
1452
1438
.. doctest ::
1453
1439
@@ -1458,8 +1444,8 @@ These can be used as types in annotations. They all support subscription using
1458
1444
>>> X.__metadata__
1459
1445
('very', 'important', 'metadata')
1460
1446
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:
1463
1449
1464
1450
.. doctest ::
1465
1451
@@ -1468,7 +1454,7 @@ These can be used as types in annotations. They all support subscription using
1468
1454
>>> Password.__origin__
1469
1455
<class 'str'>
1470
1456
1471
- Note that using :func: `get_origin ` will return ``Annotated `` itself:
1457
+ Note that using :func: `get_origin ` will return ``Annotated `` itself:
1472
1458
1473
1459
.. doctest ::
1474
1460
0 commit comments