From 96c40ea76666a107be71d10af1c3cbff35a8351b Mon Sep 17 00:00:00 2001 From: Josh <8677174+bijij@users.noreply.github.com> Date: Sun, 5 May 2024 15:45:32 +1000 Subject: [PATCH] [commands] Add support for type statement and NewType --- discord/utils.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/discord/utils.py b/discord/utils.py index a396b5615..9af6268eb 100644 --- a/discord/utils.py +++ b/discord/utils.py @@ -72,6 +72,7 @@ import string import sys from threading import Timer import types +import typing import warnings import aiohttp @@ -1202,6 +1203,7 @@ def as_chunks(iterator: _Iter[T], max_size: int) -> _Iter[List[T]]: PY_310 = sys.version_info >= (3, 10) +PY_312 = sys.version_info >= (3, 12) def flatten_literal_params(parameters: Iterable[Any]) -> Tuple[Any, ...]: @@ -1240,6 +1242,16 @@ def evaluate_annotation( cache[tp] = evaluated return evaluated + if PY_312 and getattr(tp.__repr__, '__objclass__', None) is typing.TypeAliasType: # type: ignore + temp_locals = dict(**locals, **{t.__name__: t for t in tp.__type_params__}) + annotation = evaluate_annotation(tp.__value__, globals, temp_locals, cache.copy()) + if hasattr(tp, '__args__'): + annotation = annotation[tp.__args__] + return annotation + + if hasattr(tp, '__supertype__'): + return evaluate_annotation(tp.__supertype__, globals, locals, cache) + if hasattr(tp, '__metadata__'): # Annotated[X, Y] can access Y via __metadata__ metadata = tp.__metadata__[0]