8000 gh-124120: Document `Annotated.__origin__` by sobolevn · Pull Request #124125 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix test
  • Loading branch information
sobolevn committed Sep 16, 2024
commit 2750cf836adf51a780de406b4aede4a13428be3d
12 changes: 9 additions & 3 deletions Doc/library/typing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1459,15 +1459,21 @@ These can be used as types in annotations. They all support subscription using
('very', 'important', 'metadata')

* At runtime, if you want to retrive the original
type wrapped by ``Annotated``, use :func:`get_origin`::
type wrapped by ``Annotated``, use :attr:`!__origin__` attribute:

.. doctest::

>>> from typing import Annotated, get_origin
>>> Password = Annotated[str, "secret"]
>>> get_origin(Password)
>>> Password.__origin__
<class 'str'>

Note that using :func:`get_origin` will return ``Annotated`` itself:

.. doctest::

>>> assert get_origin(Password) is Annotated

.. seealso::

:pep:`593` - Flexible function and variable annotations
Expand Down Expand Up @@ -3308,7 +3314,7 @@ Introspection helpers
assert get_origin(str) is None
assert get_origin(Dict[str, int]) is dict
assert get_origin(Union[int, str]) is Union
assert get_origin(Annotated[str, "metadata"]) is str
assert get_origin(Annotated[str, "metadata"]) is Annotated
P = ParamSpec('P')
assert get_origin(P.args) is P
assert get_origin(P.kwargs) is P
Expand Down
0