On this page
  1. typing.ParamSpec
  2. Import
  3. Signature & Overview
  4. Methods & Attributes
  5. Usage
  6. Gotchas
  7. REPL Inspect

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

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

REPL Inspect

from typing import ParamSpec
help(ParamSpec)

Linked from

Related notes