Numpy and mypy type annotation - npt.NDArray dtype not being checked

95 views Asked by At

I was trying to perform a type check for a function that accepts both float or np.ndarray[np.float].

Mypy does not complain of any of the following implementations:

from typing import Union
import numpy as np
import numpy.typing as npt

_T = Union[float, npt.NDArray[np.float64]]

def func(a: _T) -> _T:
    return a

a = np.array([1])
func(a)

b = np.array([True])
func(b)

c = np.array([""])
func(c)

Shouldn't mypy complain about the dtype being different from np.float64?

I already found workarounds for the solution of my problem. But I would like to know why the above code does not work.

0

There are 0 answers