Type hints for both integer and elements of numpy integer arrays in Python

Viewed 55

I have a function that should takes an arguments with integer values. Let's say:

def print_age_in_another_format(age: int) -> float
...

However, this code is meant to be used in an environment where numpy is heavily used so users will probably write something like:

a = np.array([10, 12, 15])
print_age_in_another_format(age=a[0]) 

(the code is not mean to be vectorized, just that users would have some integers that come from a numpy array with integers).

Question: What would be the correct way to write annotation types for these function?

So, far, the solution we have is to have something like: IntType = Union[int, np.integer]

And I am wondering if there is a more general way of addressing this or if the approach is wrong to begin with. Any pointers are welcome.

0 Answers
Related