PyCharm 中带有 nptyping 和 Array 的 Numpy Typehint

Numpy Typehint with nptyping and Array in PyCharm

我尝试将 numpy 与 nptypingArray 结合使用来进行类型提示。

我尝试了以下方法:

enemy_hand: Array[float, 48] = np.zeros(48)

我收到一个打字错误:

Expected Type 'Array[float, Any]', got 'ndarray' instead

据我了解:https://pypi.org/project/nptyping/应该是这样。

nptyping目前对静态分析没用。引用图书馆开发人员在其问题跟踪器上的 post

mypy is just not supported by nptyping (yet)

我不会对此寄予太大希望"yet"。 NumPy 的 dtype 和形状处理很难适应 typing/mypy 静态类型模型,并且 nptyping 自己的设计决策不适合 NumPy 本身。例如,开发人员似乎从未考虑过非二维数组,因此 Array[str, 3] 表示具有 3 行和未指定列的二维数组,而不是 3 元素一维数组。所有的实现也是根据行和列。

据我所知,nptyping 唯一真正的功能是 isinstance 检查,即使那也是错误的。