python3.9,removed windows build,kick vote,ban mute commands , rjcd

This commit is contained in:
imayushsaini 2021-11-10 17:26:07 +05:30
parent 94bdfb531a
commit dbe040a017
2453 changed files with 3797 additions and 437553 deletions

View file

@ -14,8 +14,7 @@ from typing import TYPE_CHECKING, cast, TypeVar, Generic
if TYPE_CHECKING:
import asyncio
from efro.call import Call as Call # 'as Call' so we re-export.
from weakref import ReferenceType
from typing import Any, Dict, Callable, Optional, Type
from typing import Any, Callable, Optional
T = TypeVar('T')
TVAL = TypeVar('TVAL')
@ -35,7 +34,7 @@ else:
Call = functools.partial
def enum_by_value(cls: Type[TENUM], value: Any) -> TENUM:
def enum_by_value(cls: type[TENUM], value: Any) -> TENUM:
"""Create an enum from a value.
This is basically the same as doing 'obj = EnumType(value)' except
@ -93,7 +92,18 @@ def utc_this_hour() -> datetime.datetime:
tzinfo=now.tzinfo)
def empty_weakref(objtype: Type[T]) -> ReferenceType[T]:
def utc_this_minute() -> datetime.datetime:
"""Get offset-aware beginning of current minute in the utc time zone."""
now = datetime.datetime.now(datetime.timezone.utc)
return datetime.datetime(year=now.year,
month=now.month,
day=now.day,
hour=now.hour,
minute=now.minute,
tzinfo=now.tzinfo)
def empty_weakref(objtype: type[T]) -> weakref.ref[T]:
"""Return an invalidated weak-reference for the specified type."""
# At runtime, all weakrefs are the same; our type arg is just
# for the static type checker.
@ -233,7 +243,7 @@ class DispatchMethodWrapper(Generic[TARG, TRET]):
def register(func: Callable[[Any, Any], TRET]) -> Callable:
"""Register a new dispatch handler for this dispatch-method."""
registry: Dict[Any, Callable]
registry: dict[Any, Callable]
# noinspection PyProtectedMember,PyTypeHints
@ -294,7 +304,7 @@ class ValueDispatcher(Generic[TVAL, TRET]):
def __init__(self, call: Callable[[TVAL], TRET]) -> None:
self._base_call = call
self._handlers: Dict[TVAL, Callable[[], TRET]] = {}
self._handlers: dict[TVAL, Callable[[], TRET]] = {}
def __call__(self, value: TVAL) -> TRET:
handler = self._handlers.get(value)
@ -325,7 +335,7 @@ class ValueDispatcher1Arg(Generic[TVAL, TARG, TRET]):
def __init__(self, call: Callable[[TVAL, TARG], TRET]) -> None:
self._base_call = call
self._handlers: Dict[TVAL, Callable[[TARG], TRET]] = {}
self._handlers: dict[TVAL, Callable[[TARG], TRET]] = {}
def __call__(self, value: TVAL, arg: TARG) -> TRET:
handler = self._handlers.get(value)
@ -370,7 +380,7 @@ def valuedispatchmethod(
# in the function call dict and simply return a call.
_base_call = call
_handlers: Dict[TVAL, Callable[[TSELF], TRET]] = {}
_handlers: dict[TVAL, Callable[[TSELF], TRET]] = {}
def _add_handler(value: TVAL, addcall: Callable[[TSELF], TRET]) -> None:
if value in _handlers:
@ -394,7 +404,8 @@ def valuedispatchmethod(
# To the type checker's eyes we return a ValueDispatchMethod instance;
# this lets it know about our register func and type-check its usage.
# In reality we just return a raw function call (for reasons listed above).
if TYPE_CHECKING: # pylint: disable=no-else-return
# pylint: disable=undefined-variable, no-else-return
if TYPE_CHECKING:
return ValueDispatcherMethod[TVAL, TRET]()
else:
return _call_wrapper
@ -427,39 +438,79 @@ def make_hash(obj: Any) -> int:
return hash(tuple(frozenset(sorted(new_obj.items()))))
def asserttype(obj: Any, typ: Type[T]) -> T:
def asserttype(obj: Any, typ: type[T]) -> T:
"""Return an object typed as a given type.
Assert is used to check its actual type, so only use this when
failures are not expected. Otherwise use checktype.
"""
assert isinstance(typ, type), 'only actual types accepted'
assert isinstance(obj, typ)
return obj
def checktype(obj: Any, typ: Type[T]) -> T:
def asserttype_o(obj: Any, typ: type[T]) -> Optional[T]:
"""Return an object typed as a given optional type.
Assert is used to check its actual type, so only use this when
failures are not expected. Otherwise use checktype.
"""
assert isinstance(typ, type), 'only actual types accepted'
assert isinstance(obj, (typ, type(None)))
return obj
def checktype(obj: Any, typ: type[T]) -> T:
"""Return an object typed as a given type.
Always checks the type at runtime with isinstance and throws a TypeError
on failure. Use asserttype for more efficient (but less safe) equivalent.
"""
assert isinstance(typ, type), 'only actual types accepted'
if not isinstance(obj, typ):
raise TypeError(f'Expected a {typ}; got a {type(obj)}.')
return obj
def warntype(obj: Any, typ: Type[T]) -> T:
def checktype_o(obj: Any, typ: type[T]) -> Optional[T]:
"""Return an object typed as a given optional type.
Always checks the type at runtime with isinstance and throws a TypeError
on failure. Use asserttype for more efficient (but less safe) equivalent.
"""
assert isinstance(typ, type), 'only actual types accepted'
if not isinstance(obj, (typ, type(None))):
raise TypeError(f'Expected a {typ} or None; got a {type(obj)}.')
return obj
def warntype(obj: Any, typ: type[T]) -> T:
"""Return an object typed as a given type.
Always checks the type at runtime and simply logs a warning if it is
not what is expected.
"""
assert isinstance(typ, type), 'only actual types accepted'
if not isinstance(obj, typ):
import logging
logging.warning('warntype: expected a %s, got a %s', typ, type(obj))
return obj # type: ignore
def warntype_o(obj: Any, typ: type[T]) -> Optional[T]:
"""Return an object typed as a given type.
Always checks the type at runtime and simply logs a warning if it is
not what is expected.
"""
assert isinstance(typ, type), 'only actual types accepted'
if not isinstance(obj, (typ, type(None))):
import logging
logging.warning('warntype: expected a %s or None, got a %s', typ,
type(obj))
return obj # type: ignore
def assert_non_optional(obj: Optional[T]) -> T:
"""Return an object with Optional typing removed.