Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specific type annotation for NumPy ndarray using mypy

Support for type annotations was added in NumPy 1.20. I'm trying to figure out how to tell mypy that an array is filled with elements of a particular type, the annotation np.ndarray[np.dcomplex] gives the mypy error "ndarray" expects no type arguments, but 1 given.

EDIT: This question is different from Type hinting / annotation (PEP 484) for numpy.ndarray as that question was asked 4 years ago when there wasn't any official support for type hinting. I'm asking for what is the official way to do this, now that type hinting is actually natively supported by numpy 1.20. The documentation at https://numpy.org/doc/stable/reference/typing.html#module-numpy.typing that the top answer there points towards only seems to say things you shouldn't do with type hinting instead of explaining what you should be doing.

like image 502
John Avatar asked Feb 24 '21 10:02

John


People also ask

What data type is NumPy Ndarray?

ndarray. An array object represents a multidimensional, homogeneous array of fixed-size items. An associated data-type object describes the format of each element in the array (its byte-order, how many bytes it occupies in memory, whether it is an integer, a floating point number, or something else, etc.)

How do I change the type of Ndarray?

We have a method called astype(data_type) to change the data type of a numpy array. If we have a numpy array of type float64, then we can change it to int32 by giving the data type to the astype() method of numpy array. We can check the type of numpy array using the dtype class.

Can NumPy Ndarray hold any type of object?

NumPy arrays are typed arrays of fixed size. Python lists are heterogeneous and thus elements of a list may contain any object type, while NumPy arrays are homogenous and can contain object of only one type.

Is NumPy array and Ndarray same?

The main data structure in NumPy is the ndarray, which is a shorthand name for N-dimensional array. When working with NumPy, data in an ndarray is simply referred to as an array. It is a fixed-sized array in memory that contains data of the same type, such as integers or floating point values.


1 Answers

What you are looking for is the numpy.typing.NDArray class: https://numpy.org/doc/stable/reference/typing.html#numpy.typing.NDArray

numpy.typing.NDArray[A] is an alias for numpy.ndarray[Any, numpy.dtype[A]]:

import numpy as np
import numpy.typing as npt

a: npt.NDArray[np.complex64] = np.zeros((3, 3), dtype=np.complex64)
# reveal_type(a)  # -> numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[numpy.typing._32Bit, numpy.typing._32Bit]]]
print(a)

prints

[[0.+0.j 0.+0.j 0.+0.j]
 [0.+0.j 0.+0.j 0.+0.j]
 [0.+0.j 0.+0.j 0.+0.j]]

Note that even though you annotate a as npt.NDArray[np.complex64], you still need to make sure that you pass the matching dtype to the factory on the right side.

a: npt.NDArray[np.complex64] = np.zeros((3, 3), dtype=np.float32)

passes the mypy check just as well.

like image 190
tgpfeiffer Avatar answered Sep 30 '22 02:09

tgpfeiffer