Typing and Generics
typing.ParamSpec
Captures a callable's parameter specification so a wrapper can forward positional and keyword arguments while preserving its static call contract. Variance and bound semantics are not standardized for ParamSpec. Defaults arrived in 3.13.
Formal Syntax & Structure
python syntaxpython
typing.ParamSpec(name, *, bound=None, covariant=False, contravariant=False, infer_variance=False, default=NoDefault)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| name | str | Parameter specification name. |
| bound | type or None | Bound metadata; static semantics unspecified. |
| covariant | bool | Variance flag; static semantics unspecified. |
| contravariant | bool | Variance flag; static semantics unspecified. |
| infer_variance | bool | Variance inference flag. |
| default | object | Default parameter specification. |
Return Value
ParamSpec
Typing construct or runtime result as described; static typing rules are not runtime validation.
Working Example
Example Snippetpython
from typing import Callable, ParamSpec, TypeVar
P = ParamSpec('P')R = TypeVar('R')def wrap(fn: Callable[P, R]) -> Callable[P, R]: def inner(*args: P.args, **kwargs: P.kwargs) -> R: return fn(*args, **kwargs) return innerdef add(a: int, *, b: int) -> int: return a+bprint(wrap(add)(2,b=3))