From 748a3c767a3781da75f00e48a85425959f13fb11 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Mon, 16 Sep 2024 16:36:30 +0300 Subject: [PATCH 1/6] gh-124120: Document use of `get_origin` to access `Annotated` origin --- Doc/library/typing.rst | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index fa7932061645a4..230f177ef245b5 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -1458,6 +1458,16 @@ These can be used as types in annotations. They all support subscription using >>> X.__metadata__ ('very', 'important', 'metadata') + * At runtime, if you want to retrive the original + type wrapped by ``Annotated``, use :func:`get_origin`:: + + .. doctest:: + + >>> from typing import Annotated, get_origin + >>> Password = Annotated[str, "secret"] + >>> get_origin(Password) + + .. seealso:: :pep:`593` - Flexible function and variable annotations From e82bb85a15685ac6c24532196126426b4b4abfd2 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Mon, 16 Sep 2024 16:44:05 +0300 Subject: [PATCH 2/6] Also mention Annotated in get_origin docs --- Doc/library/typing.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index 230f177ef245b5..e55fcfb2235518 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -3308,6 +3308,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 P = ParamSpec('P') assert get_origin(P.args) is P assert get_origin(P.kwargs) is P From 2750cf836adf51a780de406b4aede4a13428be3d Mon Sep 17 00:00:00 2001 From: sobolevn Date: Mon, 16 Sep 2024 17:03:31 +0300 Subject: [PATCH 3/6] Fix test --- Doc/library/typing.rst | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index e55fcfb2235518..00d92c7c0a850b 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -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__ + 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 @@ -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 From a9a3f74e711cf56e9926afcd0e1896bc84623d3a Mon Sep 17 00:00:00 2001 From: sobolevn Date: Mon, 16 Sep 2024 17:23:57 +0300 Subject: [PATCH 4/6] Update Doc/library/typing.rst Co-authored-by: Brian Schubert --- 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 00d92c7c0a850b..afd98b3f494bd7 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -1458,7 +1458,7 @@ These can be used as types in annotations. They all support subscription using >>> X.__metadata__ ('very', 'important', 'metadata') - * At runtime, if you want to retrive the original + * At runtime, if you want to retrieve the original type wrapped by ``Annotated``, use :attr:`!__origin__` attribute: .. doctest:: From e8de2a56147a9d8c78e05a8d0cda25c3750278d3 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Mon, 16 Sep 2024 19:20:23 +0300 Subject: [PATCH 5/6] Apply suggestions from code review Co-authored-by: Alex Waygood --- Doc/library/typing.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index afd98b3f494bd7..cb3ce0aca2353a 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -1459,7 +1459,7 @@ These can be used as types in annotations. They all support subscription using ('very', 'important', 'metadata') * At runtime, if you want to retrieve the original - type wrapped by ``Annotated``, use :attr:`!__origin__` attribute: + type wrapped by ``Annotated``, use the :attr:`!__origin__` attribute: .. doctest:: @@ -1472,7 +1472,8 @@ These can be used as types in annotations. They all support subscription using .. doctest:: - >>> assert get_origin(Password) is Annotated + >>> get_origin(Password) + Annotated .. seealso:: From 8994ed44b7e8ce0b8e6d696e61f91c57d0a1a6ff Mon Sep 17 00:00:00 2001 From: sobolevn Date: Mon, 16 Sep 2024 19:56:50 +0300 Subject: [PATCH 6/6] Fix tests --- 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 cb3ce0aca2353a..934bbd9dac14a6 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -1473,7 +1473,7 @@ These can be used as types in annotations. They all support subscription using .. doctest:: >>> get_origin(Password) - Annotated + typing.Annotated .. seealso::