From bacb1906cd481f1d66920278ea92df2dedf76e42 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Tue, 24 Sep 2024 09:53:04 +0300 Subject: [PATCH 1/2] gh-124120: Document `Annotated.__origin__` (GH-124125) (cherry picked from commit faef3fa653f2901cc905f98eae0ddcd8dc334d33) Co-authored-by: sobolevn Co-authored-by: Brian Schubert Co-authored-by: Alex Waygood --- Doc/library/typing.rst | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index bb49093505278c..21e644afcb57b3 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -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__ + + + Note that using :func:`get_origin` will return ``Annotated`` itself: + + .. doctest:: + + >>> get_origin(Password) + typing.Annotated + .. seealso:: :pep:`593` - Flexible function and variable annotations @@ -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 From 765ce9ab6456cf15faca5f83e3ded1a0972503e6 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Tue, 24 Sep 2024 06:45:28 -0700 Subject: [PATCH 2/2] Fix doctest --- Doc/library/typing.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index 21e644afcb57b3..7f86b550b915b6 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -1420,7 +1420,7 @@ These can be used as types in annotations. They all support subscription using .. doctest:: >>> get_origin(Password) - typing.Annotated + .. seealso::