Use paramspec

This commit is contained in:
DevilXD committed 2022-02-14 22:02:42 +01:00
1 parent 60a9e273c8
commit 3a5d105235
1 file changed
+27 -4
+27 -4
View File
@@ -9,14 +9,35 @@ from contextlib import suppress
from collections import OrderedDict from collections import OrderedDict
from datetime import datetime, timezone from datetime import datetime, timezone
from typing import ( from typing import (
Any, Literal, Union, List, MutableSet, Iterable, Iterator, Coroutine, Generic, TypeVar, cast Any,
Literal,
Union,
List,
MutableSet,
Callable,
Iterable,
Iterator,
Coroutine,
Generic,
TypeVar,
TYPE_CHECKING,
cast,
) )
from constants import JsonType from constants import JsonType
if TYPE_CHECKING:
from typing_extensions import ParamSpec
else:
# stub it
class ParamSpec:
def __init__(*args, **kwargs):
pass
_V = TypeVar("_V") # value _V = TypeVar("_V") # value
_D = TypeVar("_D") # default _D = TypeVar("_D") # default
_P = ParamSpec("_P") # params
logger = logging.getLogger("TwitchDrops") logger = logging.getLogger("TwitchDrops")
NONCE_CHARS = string.ascii_letters + string.digits NONCE_CHARS = string.ascii_letters + string.digits
@@ -33,11 +54,13 @@ def deduplicate(iterable: Iterable[_V]) -> List[_V]:
return list(OrderedDict.fromkeys(iterable).keys()) return list(OrderedDict.fromkeys(iterable).keys())
def task_wrapper(afunc): def task_wrapper(
afunc: Callable[_P, Coroutine[Any, Any, _V]]
) -> Callable[_P, Coroutine[Any, Any, _V]]:
@wraps(afunc) @wraps(afunc)
async def wrapper(self, *args, **kwargs): async def wrapper(*args: _P.args, **kwargs: _P.kwargs):
try: try:
await afunc(self, *args, **kwargs) await afunc(*args, **kwargs)
except Exception: except Exception:
logger.exception("Exception in task") logger.exception("Exception in task")
raise # raise up to the wrapping task raise # raise up to the wrapping task