Currently, I am checking for tuples with multiple (e.g. three) numbers of arbitrary but equal type in the following form:
from typing import Tuple, Union Union[Tuple[int, int, int], Tuple[float, float, float]]
I want to make this check more generic, also allowing
numpy number types. I.e. I tried to use
from numbers import Number from typing import Tuple Tuple[Number, Number, Number]
The above snipped also allows tuples of mixed types as long as everything is a number.
I’d like to restrict the tuple to numbers of equal type.
How can this be achieved?
Technically, this question applies to Python and the type hints specification itself. However, as pointed out in the comments, its handling is implementation specific, i.e. MyPy will not catch every edge case and/or inconsistency. Personally, I am using run-time checks with typeguard for testing and deactivate them entirely in production.