I read PEP 612 (Parameter Specification Variables)

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).

Overview

approach

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.

Example

In the example below, the argument of ʻadd_logging ()is of typeCallable [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.

Impressions

Recommended Posts

I read PEP 612 (Parameter Specification Variables)
I read PEP 613 (Explicit Type Aliases)
I read PEP 604 (Complementary syntax for Union []).
I read PEP 618 (Add Optional Length-Checking To zip)
I read PEP 614 (Relaxing Grammar Restrictions On Decorators)
I read PEP-593 (Flexible function and variable annotations)
I read PEP-544 (Protocols: Structural subtyping (static duck typing)).
I read PEP 585 (Type Hinting Generics In Standard Collections)
I read Strang's linear algebra
I read "Linux standard textbook"!
I read the SHAP paper
I read PEP 560 (Core support for typing module and generic types)