This is the second installment of the PEP series, which I heard for the first time when I was scanning the README of Microsoft's checker for Python pyright. This time, I will write about PEP 612 (Parameter Specification Variables).
ParameterSpecification
Add ParameterSpecification
as the type that represents the argument.
The ParameterSpecification
can be used in conjunction with the Callable
to behave like the generics of a callable object. It is easy to understand if you think of it as an argument version of TypeVar
.
In the example below, the argument of ʻadd_logging ()is of type
Callable [Ps, R], and the return value is also of type
Callable [Ps, R]`. That is, it is a decorator that returns a function with the same interface as the function specified as an argument.
Therefore, foo ()
decorated with @ add_logging
remains of type (x: int, y: str)-> int
.
from typing import Callable, ParameterSpecification, TypeVar
Ps = ParameterSpecification("Ps")
R = TypeVar("R")
def add_logging(f: Callable[Ps, R]) -> Callable[Ps, R]:
def inner(*args: Ps.args, **kwargs: Ps.kwargs) -> R:
log_to_database()
return f(*args, **kwargs)
return inner
@add_logging
def foo(x: int, y: str) -> int:
return x + 7
It's difficult to type these decorators if the ParameterSpecification
type isn't introduced.
ParameterSpecification
.ParameterSpecification
is a bit long, but I couldn't think of any alternatives ...Recommended Posts