Parameter specification variable.


P = ParamSpec('P')

Parameter specification variables exist primarily for the benefit of static type checkers. They are used to forward the parameter types of one callable to another callable, a pattern commonly found in higher order functions and decorators. They are only valid when used in Concatenate, or s the first argument to Callable. In Python 3.10 and higher, they are also supported in user-defined Generics at runtime. See class Generic for more information on generic types. An example for annotating a decorator::

T = TypeVar('T') P = ParamSpec('P')

def addlogging(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:'{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

Parameter specification variables defined with covariant=True or contravariant=True can be used to declare covariant or contravariant generic types. These keyword arguments are valid, but their actual semantics are yet to be decided. See PEP 612 for details.

Parameter specification variables can be introspected. e.g.: == 'T' P.bound == None P.covariant == False P.contravariant == False

Note that only parameter specification variables defined in global scope can be pickled.