summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsobolevn <mail@sobolevn.me>2024-09-24 06:53:04 (GMT)
committerGitHub <noreply@github.com>2024-09-24 06:53:04 (GMT)
commitfaef3fa653f2901cc905f98eae0ddcd8dc334d33 (patch)
tree54188b95ba035471e51079d7afeec1cd6c0f1de6
parent9d344fafc4385cb2e17425b77b54660ca83c61ac (diff)
downloadcpython-faef3fa653f2901cc905f98eae0ddcd8dc334d33.zip
cpython-faef3fa653f2901cc905f98eae0ddcd8dc334d33.tar.gz
cpython-faef3fa653f2901cc905f98eae0ddcd8dc334d33.tar.bz2
gh-124120: Document `Annotated.__origin__` (#124125)
Co-authored-by: Brian Schubert <brianm.schubert@gmail.com> Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
-rw-r--r--Doc/library/typing.rst18
1 files changed, 18 insertions, 0 deletions
diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst
index 075c58d..f52c593 100644
--- a/Doc/library/typing.rst
+++ b/Doc/library/typing.rst
@@ -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
@@ -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