Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy Typehint with nptyping and Array in PyCharm

I try to use numpy with nptyping's Array to do my typehinting.

I tried the following:

enemy_hand: Array[float, 48] = np.zeros(48)

I get an typerror:

Expected Type 'Array[float, Any]', got 'ndarray' instead

as far as I understand from this: https://pypi.org/project/nptyping/ thats how it should look.

like image 801
NameVergessen Avatar asked Dec 04 '19 15:12

NameVergessen


People also ask

How do I add Numpy arrays together?

To add the two arrays together, we will use the numpy. add(arr1,arr2) method. In order to use this method, you have to make sure that the two arrays have the same length. If the lengths of the two arrays are​ not the same, then broadcast the size of the shorter array by adding zero's at extra indexes.

What is the difference between Ndarray and array?

array is just a convenience function to create an ndarray ; it is not a class itself. You can also create an array using numpy. ndarray , but it is not the recommended way. From the docstring of numpy.


1 Answers

nptyping is currently useless for static analysis. Quoting a post by the library's developer on its issue tracker,

mypy is just not supported by nptyping (yet)

I wouldn't put much hope in that "yet". NumPy's dtype and shape handling is very hard to fit into the typing/mypy static type model, and nptyping's own design decisions are a poor fit for NumPy itself. For example, it doesn't look like the dev ever considered arrays that aren't 2D, so Array[str, 3] represents a 2D array with 3 rows and unspecified columns instead of a 3-element 1D array. All the implementation is in terms of rows and columns, too.

As far as I can tell, the only real functionality nptyping has is isinstance checks, and even that's buggy.

like image 94
user2357112 supports Monica Avatar answered Nov 09 '22 06:11

user2357112 supports Monica