numpy 与 mypy:索引 NDArray 返回 Any 类型

问题描述 投票:0回答:1

是否可以索引

numpy
数组至少接收相同的类型?

通过

mypy
运行以下代码会显示以下类型:

import numpy as np
from numpy.typing import NDArray

a = np.array([1, 2, 3], dtype=np.int64)
reveal_type(a)  # numpy.ndarray[Any, Any]

sub_a = a[:1]
reveal_type(sub_a)  # Any

b: NDArray[np.int64] = np.array([1, 2, 3], dtype=np.int64)
reveal_type(b)  # numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]]

sub_b = b[:1]
reveal_type(sub_b)  # Any

谢谢。

python arrays numpy mypy
1个回答
0
投票

最近的 numpy 和 mypy 版本似乎已修复此问题:

$ mypy --version
mypy 1.10.0 (compiled: yes)

$ pip list | grep numpy
numpy                         1.26.4

$ mypy example.py 
example.py:5: note: Revealed type is "numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy._typing._64Bit]]]"
example.py:8: note: Revealed type is "numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy._typing._64Bit]]]"
example.py:11: note: Revealed type is "numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy._typing._64Bit]]]"
example.py:14: note: Revealed type is "numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy._typing._64Bit]]]"
Success: no issues found in 1 source file
© www.soinside.com 2019 - 2024. All rights reserved.