Skip to content

Commit

Permalink
[3.12] gh-124120: Document Annotated.__origin__ (GH-124125) (#124417)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
4 people authored Sep 24, 2024
1 parent 996e409 commit 0c36c37
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Doc/library/typing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1405,6 +1405,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)
<class 'typing.Annotated'>

.. seealso::

:pep:`593` - Flexible function and variable annotations
Expand Down Expand Up @@ -3010,6 +3027,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

0 comments on commit 0c36c37

Please sign in to comment.