问题
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