When I run mypy main.py --strict
on
# main.py
import numpy as np
import numpy.typing as npt
def foo(x: npt.NDArray[np.float64]) -> npt.NDArray[np.float64]:
return np.square(x)
I get Success: no issues found in 1 source file
But when I try to calculate the np.mean
I get an error
import numpy as np
import numpy.typing as npt
def foo(x: npt.NDArray[np.float64]) -> np.float64:
return np.mean(np.square(x))
error: Returning Any from function declared to return "floating[_64Bit]" [no-any-return]
Found 1 error in 1 file (checked 1 source file)
What am I doing wrong? Is it because it doesn’t know the shape of x
, therefore np.mean
could return an array if x
was more than one-dimensional? In my case, x
is always one-dimensional therefore I always will return a float and I want the type-hint to reflect that the user should expect a single float returned.
Related – scalar/array multiplication return Any type