3.5. Typing

typing is a Python module that allows developers to specify the types of inputs to make sure the input types are correct.

3.5.1. typing.Callable: Specify an Input is of Type Function

!pip install mypy

If you want to specify an input is of type function, use typing.Callable.

%%writefile callable_example.py 

# callable_example.py 

from typing import Callable

def multiply(x: float, y: float) -> float:
    return x * y

def multiply_then_divide_by_two(multiply_func: Callable[[float, float], float], x: float, y: float) -> float:
    return multiply_func(x, y) / 2

res = multiply_then_divide_by_two(multiply, 2, 3)
$ mypy callable_example.py

Callable can now be used static type checker such as mypy to check if the input is indeed a function.

!mypy callable_example.py
Success: no issues found in 1 source file