diff --git a/setup.cfg b/setup.cfg index 740d6f1..528bc2a 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = webutils-df -version = 0.0.4 +version = 0.0.5 author = Daniil F. author_email = mail@placeholder123.to description = Miscellaneous web utilities diff --git a/src/webutils/util.py b/src/webutils/util.py index 9b6e6e6..43a6ca1 100644 --- a/src/webutils/util.py +++ b/src/webutils/util.py @@ -2,7 +2,7 @@ import logging import asyncio from functools import wraps from inspect import signature -from typing import Callable, Awaitable, Dict, Tuple, Any +from typing import Callable, Awaitable, Dict, Tuple, Any, TypeVar from aiohttp.client import ClientSession @@ -10,17 +10,16 @@ from aiohttp.client import ClientSession LOGGER_NAME = 'webutils' logger = logging.getLogger(LOGGER_NAME) +AsyncFunction = TypeVar('AsyncFunction') + def _get_param_idx_and_default(function: Callable, param_name: str) -> Tuple[int, Any]: params = signature(function).parameters return list(params.keys()).index(param_name), params[param_name].default -# TODO: Figure out why PyCharm does not produce type hints to a function decorated in this manner, -# when using the decorator without parentheses (e.g. @in_async_session instead of @in_async_session() -# in this case), as soon as the `Callable` return type is added to that decorator's signature. -def in_async_session(_func: Callable = None, *, - session_kwargs: Dict[str, Any] = None, session_param_name: str = 'session'): +def in_async_session(_func: AsyncFunction = None, *, + session_kwargs: Dict[str, Any] = None, session_param_name: str = 'session') -> AsyncFunction: """ Useful decorator for any async function that uses the `aiohttp.ClientSession` to make requests. @@ -51,7 +50,7 @@ def in_async_session(_func: Callable = None, *, if session_kwargs is None: session_kwargs = {} - def decorator(function: Callable) -> Callable: + def decorator(function: AsyncFunction) -> AsyncFunction: # Using `functools.wraps` to preserve information about the actual function being decorated # More details: https://docs.python.org/3/library/functools.html#functools.wraps @wraps(function)