On this page
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
- Requires Python 3.10+: Only valid inside
Callable[Concatenate[ArgType, P], R]for decorators that prepend/remove positional parameters.
REPL Inspect
from typing import Concatenate
help(Concatenate)
Linked from
Related notes
- tuts/python3/stdlib_module/typing/Collection Linked here; links to this page; shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/Container 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