Browse Source

Add typing for `utils.cached(_slot)_property`

pull/6753/head
Nadir Chowdhury 4 years ago
committed by GitHub
parent
commit
18badbc60f
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 49
      discord/utils.py

49
discord/utils.py

@ -21,11 +21,12 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE. DEALINGS IN THE SOFTWARE.
""" """
from __future__ import annotations
import array import array
import asyncio import asyncio
import collections.abc import collections.abc
from typing import Any, Optional, overload from typing import Any, Callable, Generic, Optional, Type, TypeVar, overload, TYPE_CHECKING
import unicodedata import unicodedata
from base64 import b64encode from base64 import b64encode
from bisect import bisect_left from bisect import bisect_left
@ -53,27 +54,43 @@ __all__ = (
) )
DISCORD_EPOCH = 1420070400000 DISCORD_EPOCH = 1420070400000
class cached_property: if TYPE_CHECKING:
def __init__(self, function): from functools import cached_property
self.function = function else:
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): def __get__(self, instance, owner):
if instance is None: if instance is None:
return self return self
value = self.function(instance)
setattr(instance, self.function.__name__, value)
value = self.function(instance) return value
setattr(instance, self.function.__name__, value)
return value FS = TypeVar('FS')
FR = TypeVar('FR', covariant=True)
CP = TypeVar('CP', bound='cached_property')
CSP = TypeVar('CSP', bound='CachedSlotProperty')
class CachedSlotProperty: class CachedSlotProperty(Generic[FS, FR]):
def __init__(self, name, function): def __init__(self, name: str, function: Callable[[FS], FR]) -> None:
self.name = name self.name = name
self.function = function self.function = function
self.__doc__ = getattr(function, '__doc__') self.__doc__ = getattr(function, '__doc__')
def __get__(self, instance, owner): @overload
def __get__(self: CSP, instance: None, owner: Type[FS]) -> CSP:
...
@overload
def __get__(self, instance: FS, owner: Type[FS]) -> FR:
...
def __get__(self, instance: Optional[FS], owner: Type[FS]) -> Any:
if instance is None: if instance is None:
return self return self
@ -84,8 +101,8 @@ class CachedSlotProperty:
setattr(instance, self.name, value) setattr(instance, self.name, value)
return value return value
def cached_slot_property(name): def cached_slot_property(name: str) -> Callable[[Callable[[FS], FR]], CachedSlotProperty[FS, FR]]:
def decorator(func): def decorator(func: Callable[[FS], FR]) -> CachedSlotProperty[FS, FR]:
return CachedSlotProperty(name, func) return CachedSlotProperty(name, func)
return decorator return decorator

Loading…
Cancel
Save