#StackBounty: #python #types #tuples #type-hinting #python-typing Tuple with multiple numbers of arbitrary but equal type

Bounty: 50

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 numbers.Number:

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.

Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.