diff options
author | Nikita Sobolev <mail@sobolevn.me> | 2023-12-02 16:10:19 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-02 16:10:19 (GMT) |
commit | a35a30509820f956d6feeaa0dbf42e9ca82c12bb (patch) | |
tree | 3aa07192d80a67f35487238398896f04644945da /Lib/typing.py | |
parent | a74daba7ca8b68f47284d82d4604721b8748bbde (diff) | |
download | cpython-a35a30509820f956d6feeaa0dbf42e9ca82c12bb.zip cpython-a35a30509820f956d6feeaa0dbf42e9ca82c12bb.tar.gz cpython-a35a30509820f956d6feeaa0dbf42e9ca82c12bb.tar.bz2 |
gh-112618: Make `Annotated` cache typed (#112619)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Diffstat (limited to 'Lib/typing.py')
-rw-r--r-- | Lib/typing.py | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/Lib/typing.py b/Lib/typing.py index b3af701..4c19aad 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -490,7 +490,7 @@ class _SpecialForm(_Final, _NotIterable, _root=True): return self._getitem(self, parameters) -class _LiteralSpecialForm(_SpecialForm, _root=True): +class _TypedCacheSpecialForm(_SpecialForm, _root=True): def __getitem__(self, parameters): if not isinstance(parameters, tuple): parameters = (parameters,) @@ -723,7 +723,7 @@ def Optional(self, parameters): arg = _type_check(parameters, f"{self} requires a single type.") return Union[arg, type(None)] -@_LiteralSpecialForm +@_TypedCacheSpecialForm @_tp_cache(typed=True) def Literal(self, *parameters): """Special typing form to define literal types (a.k.a. value types). @@ -2005,8 +2005,9 @@ class _AnnotatedAlias(_NotIterable, _GenericAlias, _root=True): return (self.__origin__,) -@_SpecialForm -def Annotated(self, params): +@_TypedCacheSpecialForm +@_tp_cache(typed=True) +def Annotated(self, *params): """Add context-specific metadata to a type. Example: Annotated[int, runtime_check.Unsigned] indicates to the @@ -2053,7 +2054,7 @@ def Annotated(self, params): where T1, T2 etc. are TypeVars, which would be invalid, because only one type should be passed to Annotated. """ - if not isinstance(params, tuple) or len(params) < 2: + if len(params) < 2: raise TypeError("Annotated[...] should be used " "with at least two arguments (a type and an " "annotation).") |