Python 3.10+typingterm
typing.ParamSpec
generics and parameters
A parameter specification variable: it captures a call signature (positional and keyword) to type decorators and wrappers (Python 3.10+).
Syntax
P = typing.ParamSpec('P')
Use it for a decorator/wrapper that must keep the wrapped function's signature: without ParamSpec you fall back to Callable[..., T], lose the parameter types, and the checker stops catching bad calls. It pairs with Callable and Concatenate and needs Python 3.10+ (typing_extensions on older versions).
Examples
from typing import ParamSpec
P = ParamSpec('P')
print(P.__name__) # → P
print(P.args) # → P.args
print(P.kwargs) # → P.kwargs
def wrapper(*args: P.args, **kwargs: P.kwargs) -> None: pass
print(wrapper.__annotations__['args']) # → P.args
See also
Open in the full glossarydocs.python.orgReport an inaccuracy