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

Skip to content

gh-124120: Document Annotated.__origin__ #124125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 24, 2024
Merged
Changes from all commits
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
18 changes: 18 additions & 0 deletions Doc/library/typing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1458,6 +1458,23 @@ These can be used as types in annotations. They all support subscription using
>>> X.__metadata__
('very', 'important', 'metadata')

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

.. doctest::

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

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

.. doctest::

>>> get_origin(Password)
typing.Annotated

.. seealso::

:pep:`593` - Flexible function and variable annotations
Expand Down Expand Up @@ -3298,6 +3315,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 Annotated
P = ParamSpec('P')
assert get_origin(P.args) is P
assert get_origin(P.kwargs) is P
Expand Down
Loading
0