I am trying to determine if the following mypy warning is telling me something important.
import numpy as np
from numpy import typing as npt
def test(a: npt.ArrayLike) -> npt.NDArray:
a = np.asarray(a)
a = np.abs(a)
return a
This results in the following warning on the return a
line.
Incompatible return value type (got “_SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]”, expected “ndarray[Any, dtype[Any]]”)Mypyreturn-value
Both the following work without warnings:
return np.abs(a)
b = np.abs(a)
return b
So I can’t tell if there really is an issue with reassigning a
to itself in np.abs
or if this is one of the many subtleties of numpy
typing that I should just work around. The warning remains with many other numpy
functions (np.sin
,np.ceil
, etc.).