]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-124120: Document `Annotated.__origin__` (GH-124125) (#124416)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 24 Sep 2024 18:34:21 +0000 (20:34 +0200)
committerGitHub <noreply@github.com>
Tue, 24 Sep 2024 18:34:21 +0000 (11:34 -0700)
gh-124120: Document `Annotated.__origin__` (GH-124125)
(cherry picked from commit faef3fa653f2901cc905f98eae0ddcd8dc334d33)

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>
Doc/library/typing.rst

index 7f8e246a6481909c4c7d7f7ae1b2b3903a374d56..c158b207bf61951071af5cecfff20ecd95588184 100644 (file)
@@ -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
@@ -3222,6 +3239,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