Search code examples
pythonpython-typing

what exactly is python typing.Callable?


I have seen typing.Callable, but I didn't find any useful docs about it. What exactly is typing.Callable?


Solution

  • typing.Callable is the type you use to indicate a callable. Most python types that support the () operator are of the type collections.abc.Callable. Examples include functions, classmethods, staticmethods, bound methods and lambdas.

    In summary, anything with a __call__ method (which is how () is implemented), is a callable.

    PEP 677 attempted to introduce implicit tuple-with-arrow syntax, so that something like Callable[[int, str], list[float]] could be expressed much more intuitively as (int, str) -> list[float]. The PEP was rejected because the benefits of the new syntax were not deemed sufficient given the added maintenance burden and possible room for confusion.