Skip to content

Commit faef3fa

Browse files
sobolevnbrianschubertAlexWaygood
authored
gh-124120: Document Annotated.__origin__ (#124125)
Co-authored-by: Brian Schubert <[email protected]> Co-authored-by: Alex Waygood <[email protected]>
1 parent 9d344fa commit faef3fa

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Doc/library/typing.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,6 +1458,23 @@ These can be used as types in annotations. They all support subscription using
14581458
>>> X.__metadata__
14591459
('very', 'important', 'metadata')
14601460

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
1467+
>>> Password = Annotated[str, "secret"]
1468+
>>> Password.__origin__
1469+
<class 'str'>
1470+
1471+
Note that using :func:`get_origin` will return ``Annotated`` itself:
1472+
1473+
.. doctest::
1474+
1475+
>>> get_origin(Password)
1476+
typing.Annotated
1477+
14611478
.. seealso::
14621479

14631480
:pep:`593` - Flexible function and variable annotations
@@ -3298,6 +3315,7 @@ Introspection helpers
32983315
assert get_origin(str) is None
32993316
assert get_origin(Dict[str, int]) is dict
33003317
assert get_origin(Union[int, str]) is Union
3318+
assert get_origin(Annotated[str, "metadata"]) is Annotated
33013319
P = ParamSpec('P')
33023320
assert get_origin(P.args) is P
33033321
assert get_origin(P.kwargs) is P

0 commit comments

Comments
 (0)