mypy is really handy and catches a lot of bugs, but when I write \"scientific\" applications, I often end up doing:
def my_func(number: Union[float, int]):
#
For people who come to this question for the more general problem of Union typing hints for entities which don't have an existing supertype in common, for example Union[int, numpy.ndarray]
, the solution is to import Union
from typing
.
Example 1:
from typing import Union
def my_func(number: Union[float, int]):
# Do something
Example 2:
from typing import Union
import numpy as np
def my_func(x: Union[float, np.ndarray]):
# do something
# Do something