Closed
Description
Currently we use a custom mypy
plugin to fix typings of some decorators.
Plugin: https://github.com/dry-python/returns/blob/master/returns/contrib/mypy/_features/decorators.py
Progress: python/mypy#8645
PEP: https://www.python.org/dev/peps/pep-0612/
Source: https://github.com/python/cpython/blob/366dc3a1354078e38808b9c16276e97cca5b8aaf/Lib/typing.py#L730
Example:
from typing_extensions import ParamSpec
from typing import TypeVar, Callable
T = TypeVar('T')
P = ParamSpec('P')
def add_logging(f: Callable[P, T]) -> Callable[P, T]:
'''A type-safe decorator to add logging to a function.'''
def inner(*args: P.args, **kwargs: P.kwargs) -> T:
logging.info(f'{f.__name__} was called')
return f(*args, **kwargs)
return inner
@add_logging
def add_two(x: float, y: float) -> float:
'''Add two numbers together.'''
return x + y
It is not working right now, we need to wait until proper support is merged into mypy.
Activity
thepabloaguilar commentedon Feb 19, 2021
That's beautiful!!
thepabloaguilar commentedon Feb 19, 2021
Refs #264
@safe(exceptions=(TypeError, ValueError))
variant #605ParamSpec
#1196