Skip to content

Commit 8c0c344

Browse files
miss-islingtonsobolevnbrianschubertAlexWaygood
authored
[3.13] pythongh-124120: Document Annotated.__origin__ (pythonGH-124125) (python#124416)
pythongh-124120: Document `Annotated.__origin__` (pythonGH-124125) (cherry picked from commit faef3fa) Co-authored-by: sobolevn <[email protected]> Co-authored-by: Brian Schubert <[email protected]> Co-authored-by: Alex Waygood <[email protected]>
1 parent e5105cc commit 8c0c344

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
@@ -3222,6 +3239,7 @@ Introspection helpers
32223239
assert get_origin(str) is None
32233240
assert get_origin(Dict[str, int]) is dict
32243241
assert get_origin(Union[int, str]) is Union
3242+
assert get_origin(Annotated[str, "metadata"]) is Annotated
32253243
P = ParamSpec('P')
32263244
assert get_origin(P.args) is P
32273245
assert get_origin(P.kwargs) is P

0 commit comments

Comments
 (0)