Skip to content

Limitations of static typing 0D arrays #19064

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
BvB93 opened this issue May 22, 2021 · 1 comment
Closed

Limitations of static typing 0D arrays #19064

BvB93 opened this issue May 22, 2021 · 1 comment

Comments

@BvB93
Copy link
Member

BvB93 commented May 22, 2021

During runtime numpy aggressively casts any passed 0D arrays into their corresponding generic instance. Until the introduction of shape typing (see PEP 646) it is unfortunately not possible to make the necessary distinction between 0D and >0D arrays. While thus not strictly correct, all operations that can potentially perform a 0D-array -> scalar cast are currently annotated as exclusively returning an ndarray.

If it is known in advance that an operation will perform a 0D-array -> scalar cast, then one can consider manually remedying the situation with either typing.cast or a # type: ignore comment.

Examples

from typing import cast
import numpy as np

ar_i8 = np.array(1, dtype=np.int64)

# This will yield a `float64` during runtime, but is currently annotated as returning an `ndarray`.
# The output type is manually corrected here via `typing.cast`.
f8 = cast(np.float64, ar_i8 + 1.0)

See Also

#16544: The more general issue on array shape-typing.

@jorenham
Copy link
Member

I don't think this is relevant anymore:

>>> import numpy as np
>>> np.array(1, dtype=np.int64)
array(1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants