Structured array allows non-integer size but shouldn't #3018

Closed
gerritholl opened this Issue Feb 26, 2013 · 1 comment

Projects

None yet

2 participants

@gerritholl
Contributor

The creation of a dtype where any size is not a strictly positive integer should raise an error (ValueError or TypeError). In numpy 1.8.0.dev-b8bfcd0 or 1.6.2, it gets instead silently truncated when an object with this

In [165]: zeros(10, dtype=[("A", np.float32, 2.4)])
Out[165]: 
array([([0.0, 0.0],), ([0.0, 0.0],), ([0.0, 0.0],), ([0.0, 0.0],),
       ([0.0, 0.0],), ([0.0, 0.0],), ([0.0, 0.0],), ([0.0, 0.0],),
       ([0.0, 0.0],), ([0.0, 0.0],)], 
      dtype=[('A', '<f4', (2.4,))])

The same for ones or empty. A different case, that probably boils down to the same problem:

In [167]: zeros(10, dtype=[("A", np.float32, -1)])
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-167-03c3eaec78f1> in <module>()
----> 1 zeros(10, dtype=[("A", np.float32, -1)])

ValueError: array is too big.

That probably shouldn't say too big, but invalid.

When I pass a complex number, numpy does complain about invalid shape:

In [172]: zeros(10, dtype=[("A", np.float32, 1j)])
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-172-d5fa929505ed> in <module>()
----> 1 zeros(10, dtype=[("A", np.float32, 1j)])

ValueError: invalid shape in fixed-type tuple.

I guess it should raise the same ValueError "invalid shape" for any size that is not a strictly positive integer.

Tested with the latest build, version 1.8.0.dev-b8bfcd0, git_revision b8bfcd0.

@seberg
Member
seberg commented Dec 5, 2013

Deprecation is already started (though extremely spammy, because the float is stored badly in 1.8.x, and the deprecation is given almost every time the dtype is used). gh-4095 will fix that though and safe a clean integer into the dtype.

EDIT: Goes also for the negative, etc. shape issues.

@seberg seberg closed this Dec 5, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment