[3.12] gh-112618: Make Annotated cache typed (#112619) (#112628)

This commit is contained in:
Alex Waygood 2023-12-03 00:41:03 +00:00 committed by GitHub
parent e3c7947770
commit 2a378ca2ef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 2 deletions

View file

@ -2065,9 +2065,14 @@ class Annotated:
def __new__(cls, *args, **kwargs):
raise TypeError("Type Annotated cannot be instantiated.")
@_tp_cache
def __class_getitem__(cls, params):
if not isinstance(params, tuple) or len(params) < 2:
if not isinstance(params, tuple):
params = (params,)
return cls._class_getitem_inner(cls, *params)
@_tp_cache(typed=True)
def _class_getitem_inner(cls, *params):
if len(params) < 2:
raise TypeError("Annotated[...] should be used "
"with at least two arguments (a type and an "
"annotation).")