aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRapptz <[email protected]>2021-04-18 08:02:52 -0400
committerRapptz <[email protected]>2021-04-18 08:07:11 -0400
commitcc4dced7c0aa8c4ab1a99bc7563bb002dfe3873d (patch)
treedec393e0aca3ab7819635106fc9fea7b647be6e6
parentAdd typing for `utils.cached(_slot)_property` (diff)
downloaddiscord.py-cc4dced7c0aa8c4ab1a99bc7563bb002dfe3873d.tar.xz
discord.py-cc4dced7c0aa8c4ab1a99bc7563bb002dfe3873d.zip
Cleanup some of the prior typings for cached_slot_property
-rw-r--r--discord/utils.py45
1 files changed, 22 insertions, 23 deletions
diff --git a/discord/utils.py b/discord/utils.py
index 8e588f7f..c756260e 100644
--- a/discord/utils.py
+++ b/discord/utils.py
@@ -54,43 +54,42 @@ __all__ = (
)
DISCORD_EPOCH = 1420070400000
-if TYPE_CHECKING:
- from functools import cached_property
-else:
- class cached_property:
- def __init__(self, function):
- self.function = function
- self.__doc__ = getattr(function, '__doc__')
+class cached_property:
+ def __init__(self, function):
+ self.function = function
+ self.__doc__ = getattr(function, '__doc__')
+
+ def __get__(self, instance, owner):
+ if instance is None:
+ return self
- def __get__(self, instance, owner):
- if instance is None:
- return self
+ value = self.function(instance)
+ setattr(instance, self.function.__name__, value)
- value = self.function(instance)
- setattr(instance, self.function.__name__, value)
+ return value
- return value
+if TYPE_CHECKING:
+ from functools import cached_property
-FS = TypeVar('FS')
-FR = TypeVar('FR', covariant=True)
-CP = TypeVar('CP', bound='cached_property')
+T = TypeVar('T')
+T_co = TypeVar('T_co', covariant=True)
CSP = TypeVar('CSP', bound='CachedSlotProperty')
-class CachedSlotProperty(Generic[FS, FR]):
- def __init__(self, name: str, function: Callable[[FS], FR]) -> None:
+class CachedSlotProperty(Generic[T, T_co]):
+ def __init__(self, name: str, function: Callable[[T], T_co]) -> None:
self.name = name
self.function = function
self.__doc__ = getattr(function, '__doc__')
@overload
- def __get__(self: CSP, instance: None, owner: Type[FS]) -> CSP:
+ def __get__(self: CSP, instance: None, owner: Type[T]) -> CSP:
...
@overload
- def __get__(self, instance: FS, owner: Type[FS]) -> FR:
+ def __get__(self, instance: T, owner: Type[T]) -> T_co:
...
- def __get__(self, instance: Optional[FS], owner: Type[FS]) -> Any:
+ def __get__(self, instance: Optional[T], owner: Type[T]) -> Any:
if instance is None:
return self
@@ -101,8 +100,8 @@ class CachedSlotProperty(Generic[FS, FR]):
setattr(instance, self.name, value)
return value
-def cached_slot_property(name: str) -> Callable[[Callable[[FS], FR]], CachedSlotProperty[FS, FR]]:
- def decorator(func: Callable[[FS], FR]) -> CachedSlotProperty[FS, FR]:
+def cached_slot_property(name: str) -> Callable[[Callable[[T], T_co]], CachedSlotProperty[T, T_co]]:
+ def decorator(func: Callable[[T], T_co]) -> CachedSlotProperty[T, T_co]:
return CachedSlotProperty(name, func)
return decorator