NumPy ndarray dtype的类型提示? [英] Type hint for NumPy ndarray dtype?
问题描述
我希望函数在NumPy ndarray
的dtype
旁边包含类型提示.
I would like a function to include a type hint for NumPy ndarray
's alongside with its dtype
.
例如,使用列表,可以执行以下操作...
With lists, for example, one could do the following...
def foo(bar: List[int]):
...
...为了给出类型提示,bar
必须是由int
组成的list
.
...in order to give a type hint that bar
has to be list
consisting of int
's.
不幸的是,此语法引发了NumPy ndarray
的异常:
Unfortunately, this syntax throws exceptions for NumPy ndarray
:
def foo(bar: np.ndarray[np.bool]):
...
> np.ndarray[np.bool]) (...) TypeError: 'type' object is not subscriptable
是否可以为np.ndarray
提供特定于dtype
的类型提示?
Is it possible to give dtype
-specific type hints for np.ndarray
?
推荐答案
据我所知,尚无法在函数签名中的numpy数组类型提示中指定dtype
.计划在将来的某个时候实施.参见 numpy GitHub第#7370 和
To the best of my knowledge it's not possible yet to specify dtype
in numpy array type hints in function signatures. It is planned to be implemented at some point in the future. See numpy GitHub issue #7370 and numpy-stubs GitHub for more details on the current development status.
这篇关于NumPy ndarray dtype的类型提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!