Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ParamSpec #823

Closed
sobolevn opened this issue Feb 17, 2021 · 2 comments · Fixed by #1196
Closed

Support ParamSpec #823

sobolevn opened this issue Feb 17, 2021 · 2 comments · Fixed by #1196
Labels
enhancement New feature or request

Comments

@sobolevn
Copy link
Member

Currently we use a custom mypy plugin to fix typings of some decorators.
Plugin: https:/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:/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.

@sobolevn sobolevn added the enhancement New feature or request label Feb 17, 2021
@thepabloaguilar
Copy link
Member

That's beautiful!!

@thepabloaguilar
Copy link
Member

Refs #264

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

Successfully merging a pull request may close this issue.

2 participants