On this page
  1. typing.Concatenate
  2. Import
  3. Signature & Overview
  4. Usage
  5. Gotchas
  6. REPL Inspect

typing.Concatenate

Prev: typing.Collection | Up: typing | Next: typing.Container
Rel: typing.AbstractSet, typing.Annotated, typing.Any, typing.assert_never
Ref: docs.python.org — typing.Concatenate
Tags: #public #python3 #stdlib #typing


Import

import typing
from typing import Concatenate

Signature & Overview

typing.Concatenate(*args, **kwds)

Special form for annotating higher-order functions.

Usage

from typing import Callable, Concatenate, ParamSpec, TypeVar

P = ParamSpec("P")
R = TypeVar("R")

def with_prefix(fn: Callable[Concatenate[str, P], R]) -> Callable[P, R]:
    def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
        return fn("PREFIX", *args, **kwargs)
    return wrapper

@with_prefix
def join(prefix: str, text: str) -> str:
    return f"{prefix}:{text}"

assert join("ok") == "PREFIX:ok"

Gotchas

REPL Inspect

from typing import Concatenate
help(Concatenate)

Linked from

Related notes