Callable type; Callable[[int], str]
is a function of (int) -> str.
The subscription syntax must always be used with exactly two values: the argument list and the return type. The argument list must be a list of types or an ellipsis; the return type must be a single type.
There is no syntax to indicate optional or keyword arguments;
such function types are rarely used as callback types.
Callable[..., ReturnType]
(literal ellipsis) can be used to
type hint a callable taking any number of arguments and returning
ReturnType
. A plain Callable
is equivalent to
Callable[..., Any]
, and in turn to
collections.abc.Callable
.
Callables which take other callables as arguments may indicate that their
parameter types are dependent on each other using ParamSpec
.
Additionally, if that callable adds or removes arguments from other
callables, the Concatenate
operator may be used. They
take the form Callable[ParamSpecVariable, ReturnType]
and
Callable[Concatenate[Arg1Type, Arg2Type, ..., ParamSpecVariable], ReturnType]
respectively.
Deprecated since version 3.9: collections.abc.Callable
now supports []
. See PEP 585 and
Generic Alias Type.
Changed in version 3.10: Callable
now supports ParamSpec
and Concatenate
.
See PEP 612 for more information.
from: https://docs.python.org/3/library/typing.html#typing.Callable
No comments:
Post a Comment