Skip to content

Support ParamSpec #823

@sobolevn

Description

@sobolevn

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions