NumPy type hint that something is both an array and float32? [duplicate]

大兔子大兔子 提交于 2021-01-29 12:55:54

问题


How can I type hint that a value being returned by a function is both an NumPy array and holds NumPy float32 data?

I can specify that the returned value is an array using:

def func() -> np.ndarray:
    ...

However, this does not enforce the knowledge that it is a float32 array.

I can specify that the returned value is of type float32 using:

def func() -> np.float32:
    ...

However, this does not enforce the knowledge that it is an array (as opposed to a singular scalar value).

Is there a way I can get both specified in the return type?


回答1:


This is currently impossible but is a popular feature request; see this GitHub issue which contains information on progress and possible workarounds.



来源:https://stackoverflow.com/questions/58041659/numpy-type-hint-that-something-is-both-an-array-and-float32

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!