Python 3 type hints for function signature

For that purpose, use the typing.Callable type (see here):

from typing import Callable

def takes_two(f: Callable[[int, int], int]) -> int:
    return f(123, 456)

The first argument to Callable is a list of types for the arguments of the function, while the second argument is the return type.

Of course, python itself does not check types at all. For this, you should use additional tools such as mypy


Short answer: there is no built-in way to enforce type declaration and checking.

As the name suggests, these are type hints, which can help the programmer know what is passed as arguments and what is returned from functions (this can be specially useful when reading/reviewing large code files). However, as can be seen here in this post from Guido, Jukka and Lukasz:

It should also be emphasized that Python will remain a dynamically typed language, and the authors have no desire to ever make type hints mandatory, even by convention.

So if you expect statically-typed arguments which would raise errors if passed objects do not have the required type, then you should not be using python in the first place.

However, you may have some options: e.g. IDEs, like pycharm or Atom, have plug-ins that will check types for you.


However, if your point is to just have a type hint for callable with no enforcing, error-raising or automatic checking, then check @dseuss' answer :)