NumPy ndarray dtype的类型提示? [英] Type hint for NumPy ndarray dtype?

查看:305
本文介绍了NumPy ndarray dtype的类型提示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望函数在NumPy ndarraydtype旁边包含类型提示.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆