From 90106965fa574b229bdc034236a9626fb8b2a2f4 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Mon, 10 Apr 2023 10:58:55 +0300 Subject: [PATCH 1/3] gh-97797: Mention `__metadata__` in docstrings of `typing.{_AnnotatedAlias, Annotated}` --- Lib/typing.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Lib/typing.py b/Lib/typing.py index 1f1c4ffa2566ab..27f50b82dcbcd7 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -2154,6 +2154,8 @@ class _AnnotatedAlias(_NotIterable, _GenericAlias, _root=True): with extra annotations. The alias behaves like a normal typing alias, instantiating is the same as instantiating the underlying type, binding it to types is also the same. + + The metadata itself is storred in '__metadata__' attribute as a tuple. """ def __init__(self, origin, metadata): if isinstance(origin, _AnnotatedAlias): @@ -2209,6 +2211,10 @@ class Annotated: Details: - It's an error to call `Annotated` with less than two arguments. + - Metadata can be accessed in runtime with:: + + Annotated[int, '$'].__metadata__ == ('$',) + - Nested Annotated are flattened:: Annotated[Annotated[T, Ann1, Ann2], Ann3] == Annotated[T, Ann1, Ann2, Ann3] From e701d0f953d3708887774fd8645cb6f66309eae1 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Mon, 10 Apr 2023 09:36:43 +0100 Subject: [PATCH 2/3] Fix typo Co-authored-by: Kirill <80244920+Eclips4@users.noreply.github.com> --- Lib/typing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/typing.py b/Lib/typing.py index 27f50b82dcbcd7..f9439504099d0d 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -2155,7 +2155,7 @@ class _AnnotatedAlias(_NotIterable, _GenericAlias, _root=True): instantiating is the same as instantiating the underlying type, binding it to types is also the same. - The metadata itself is storred in '__metadata__' attribute as a tuple. + The metadata itself is stored in '__metadata__' attribute as a tuple. """ def __init__(self, origin, metadata): if isinstance(origin, _AnnotatedAlias): From 428765033e64ce49a44127a18ac9fc16945205c5 Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Mon, 10 Apr 2023 16:47:11 +0300 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: Alex Waygood --- Lib/typing.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/typing.py b/Lib/typing.py index f9439504099d0d..7c165562c2b53d 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -2155,7 +2155,7 @@ class _AnnotatedAlias(_NotIterable, _GenericAlias, _root=True): instantiating is the same as instantiating the underlying type, binding it to types is also the same. - The metadata itself is stored in '__metadata__' attribute as a tuple. + The metadata itself is stored in a '__metadata__' attribute as a tuple. """ def __init__(self, origin, metadata): if isinstance(origin, _AnnotatedAlias): @@ -2211,7 +2211,7 @@ class Annotated: Details: - It's an error to call `Annotated` with less than two arguments. - - Metadata can be accessed in runtime with:: + - Access the metadata via the ``__metadata__`` attribute:: Annotated[int, '$'].__metadata__ == ('$',)