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]
|
||||
name = webutils-df
|
||||
version = 0.0.4
|
||||
version = 0.0.5
|
||||
author = Daniil F.
|
||||
author_email = mail@placeholder123.to
|
||||
description = Miscellaneous web utilities
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user