多次赋值后NumPy变量出现Mypy类型错误,求解析
关于Mypy对NumPy自赋值代码的警告疑问
我想确认以下Mypy警告是否具有重要提示意义。
代码示例:
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
上述代码在return a行触发如下警告:
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
而以下两种写法均无警告:
return np.abs(a)
b = np.abs(a) return b
我无法判断是对a进行自我赋值(如a = np.abs(a))存在问题,还是这属于NumPy类型标注的特性需要规避。该警告在使用np.sin、np.ceil等其他NumPy函数时也会出现。
内容的提问来源于stack exchange,提问作者user22524802
相关产品推荐
相关产品推荐

