fixed pycharm type hint problem by using a TypeVar
for the annotation of the decorated function
This commit is contained in:
parent
c4362a7b26
commit
73133e3f74
@ -1,6 +1,6 @@
|
|||||||
[metadata]
|
[metadata]
|
||||||
name = webutils-df
|
name = webutils-df
|
||||||
version = 0.0.4
|
version = 0.0.5
|
||||||
author = Daniil F.
|
author = Daniil F.
|
||||||
author_email = mail@placeholder123.to
|
author_email = mail@placeholder123.to
|
||||||
description = Miscellaneous web utilities
|
description = Miscellaneous web utilities
|
||||||
|
@ -2,7 +2,7 @@ import logging
|
|||||||
import asyncio
|
import asyncio
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
from inspect import signature
|
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
|
from aiohttp.client import ClientSession
|
||||||
|
|
||||||
@ -10,17 +10,16 @@ from aiohttp.client import ClientSession
|
|||||||
LOGGER_NAME = 'webutils'
|
LOGGER_NAME = 'webutils'
|
||||||
logger = logging.getLogger(LOGGER_NAME)
|
logger = logging.getLogger(LOGGER_NAME)
|
||||||
|
|
||||||
|
AsyncFunction = TypeVar('AsyncFunction')
|
||||||
|
|
||||||
|
|
||||||
def _get_param_idx_and_default(function: Callable, param_name: str) -> Tuple[int, Any]:
|
def _get_param_idx_and_default(function: Callable, param_name: str) -> Tuple[int, Any]:
|
||||||
params = signature(function).parameters
|
params = signature(function).parameters
|
||||||
return list(params.keys()).index(param_name), params[param_name].default
|
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,
|
def in_async_session(_func: AsyncFunction = None, *,
|
||||||
# when using the decorator without parentheses (e.g. @in_async_session instead of @in_async_session()
|
session_kwargs: Dict[str, Any] = None, session_param_name: str = 'session') -> AsyncFunction:
|
||||||
# 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'):
|
|
||||||
"""
|
"""
|
||||||
Useful decorator for any async function that uses the `aiohttp.ClientSession` to make requests.
|
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:
|
if session_kwargs is None:
|
||||||
session_kwargs = {}
|
session_kwargs = {}
|
||||||
|
|
||||||
def decorator(function: Callable) -> Callable:
|
def decorator(function: AsyncFunction) -> AsyncFunction:
|
||||||
# Using `functools.wraps` to preserve information about the actual function being decorated
|
# Using `functools.wraps` to preserve information about the actual function being decorated
|
||||||
# More details: https://docs.python.org/3/library/functools.html#functools.wraps
|
# More details: https://docs.python.org/3/library/functools.html#functools.wraps
|
||||||
@wraps(function)
|
@wraps(function)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user