On this page
typing.ParamSpec
Prev: typing.NewType | Up: typing | Next: typing.ParamSpecArgs
Rel: typing.AbstractSet, typing.Annotated, typing.Any, typing.assert_never
Ref: docs.python.org — typing.ParamSpec
Tags: #public #python3 #stdlib #typing
Import
import typing
from typing import ParamSpec
Signature & Overview
typing.ParamSpec(...)
Parameter specification variable.
Methods & Attributes
ParamSpec.has_default(self, /)—ParamSpec.has_defaultmethod/property.
Usage
from functools import wraps
from typing import Callable, ParamSpec, TypeVar
P = ParamSpec("P")
R = TypeVar("R")
def trace(fn: Callable[P, R]) -> Callable[P, R]:
@wraps(fn)
def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
return fn(*args, **kwargs)
return wrapper
@trace
def add(a: int, b: int) -> int:
return a + b
assert add(2, 3) == 5
Gotchas
- Requires Python 3.10+: Preserves exact parameter names, types, and defaults across decorators (
P.argsandP.kwargsmust always be used together).
REPL Inspect
from typing import ParamSpec
help(ParamSpec)
Linked from
Related notes
- tuts/python3/stdlib_module/typing/NewType Linked here; links to this page; shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/ParamSpecArgs Linked here; links to this page; shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/typing/AbstractSet Linked here; shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/Annotated Linked here; shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/Any Linked here; shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/assert_never Linked here; shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/assert_type shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/AsyncContextManager shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/AsyncGenerator shared tags: #stdlib, #typing