-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Add basic dtype typing support to the main numpy namespace #19252
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
Comments
We are interested in this one and following this issue :), are there any ideas on supporting this when one needs to specify the byteorder? Following your previous example:
How would you write (3) so that mypy understands what it is without having to explicitly define the type for variable arr3? |
it's more contrived then I'd like it to be, but you could take of advantage of the arr3 = np.array(1, dtype=np.dtype(">f8")) |
Though
Thanks for the suggestion |
Is this the right issue for tracking the following problem? from typing import Any
import numpy as np
import numpy.typing as npt
RealArray = npt.NDArray[np.floating[Any]]
x: RealArray = np.zeros(10)
reveal_type(x)
reveal_type(np.square(x))
reveal_type(np.square(x) + np.square(x)) gives ❯ pyright a.py
/home/neil/src/efax/a.py:8:13 - information: Type of "x" is "ndarray[Any, dtype[floating[_64Bit]]]"
/home/neil/src/efax/a.py:9:13 - information: Type of "np.square(x)" is "ndarray[Any, dtype[Any]]"
/home/neil/src/efax/a.py:10:13 - information: Type of "np.square(x) + np.square(x)" is "ndarray[Any, dtype[bool_]]"
0 errors, 0 warnings, 3 informations
Completed in 0.547sec
❯ mypy a.py
a.py:8: note: Revealed type is "numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]]"
a.py:9: note: Revealed type is "numpy.ndarray[Any, numpy.dtype[Any]]"
a.py:10: note: Revealed type is "Any" |
@BvB93 Should I file a new issue for the above problem? |
Uh oh!
There was an error while loading. Please reload this page.
Back in #17719 the first steps were taken into introducing static typing support for array dtypes.
Since the dtype has a substantial effect on the semantics of an array, there is a lot of type-safety
to be gained if the various function-annotations in numpy can actually utilize this information.
Examples of this would be the rejection of string-arrays for arithmetic operations, or inferring the
output dtype of mixed float/integer operations.
The Plan
With this in mind I'd ideally like to implement some basic dtype support throughout the main numpy
namespace (xref #16546) before the release of 1.22.
Now, what does "basic" mean in this context? Namely, any array-/dtype-like that can be parametrized
w.r.t.
np.generic
. Notably this excludes builtin scalar types and character codes (literal strings), as theonly way of implementing the latter two is via excessive use of overloads.
With this in mind, I realistically only expect dtype-support for builtin scalar types (e.g.
func(..., dtype=float)
)to-be added with the help of a mypy plugin, e.g. via injecting a type-check-only method into the likes of
builtins.int
that holds some sort of explicit reference tonp.int_
.Examples
Two examples wherein the dtype can be automatically inferred:
Three examples wherein dtype-support is substantially more difficult to implement.
In the latter three cases one can always manually declare the dtype of the array:
The text was updated successfully, but these errors were encountered: