8000 [3.12] gh-124120: Document `Annotated.__origin__` (GH-124125) (#124417) · python/cpython@0c36c37 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0c36c37

Browse files
miss-islingtonsobolevnbrianschubertAlexWaygood
authored
[3.12] gh-124120: Document Annotated.__origin__ (GH-124125) (#124417)
gh-124120: Document `Annotated.__origin__` (GH-124125) (cherry picked from commit faef3fa) Co-authored-by: sobolevn <mail@sobolevn.me> Co-authored-by: Brian Schubert <brianm.schubert@gmail.com> Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
1 parent 996e409 commit 0c36c37

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
@@ -1405,6 +1405,23 @@ These can be used as types in annotations. They all support subscription using
14051405
>>> X.__metadata__
14061406
('very', 'important', 'metadata')
14071407

1408+
* At runtime, if you want to retrieve the original
1409+
type wrapped by ``Annotated``, use the :attr:`!__origin__` attribute:
1410+
1411+
.. doctest::
1412+
1413+
>>> from typing import Annotated, get_origin
1414+
>>> Password = Annotated[str, "secret"]
1415+
>>> Password.__origin__
1416+
<class 'str'>
1417+
1418+
Note that using :func:`get_origin` will return ``Annotated`` itself:
1419+
1420+
.. doctest::
1421+
1422+
>>> get_origin(Password)
1423+
<class 'typing.Annotated'>
1424+
14081425
.. seealso::
14091426

14101427
:pep:`593` - Flexible function and variable annotations
@@ -3010,6 +3027,7 @@ Introspection helpers
30103027
assert get_origin(str) is None
30113028
assert get_origin(Dict[str, int]) is dict
30123029
assert get_origin(Union[int, str]) is Union
3030+
assert get_origin(Annotated[str, "metadata"]) is Annotated
30133031
P = ParamSpec('P')
30143032
assert get_origin(P.args) is P
30153033
assert get_origin(P.kwargs) is P

0 commit comments

Comments
 (0)
0