Skip to content
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

BUG: Make dtype.descr error for out-of-order fields. #10562

Merged
merged 3 commits into from Feb 9, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions numpy/core/_internal.py
Expand Up @@ -110,6 +110,10 @@ def _array_descr(descriptor):
num = field[1] - offset
result.append(('', '|V%d' % num))
offset += num
elif field[1] < offset:
raise ValueError(
"dtype.descr is not defined for types with overlapping or "
"out-of-order fields")
if len(field) > 3:
name = (field[2], field[3])
else:
Expand Down
13 changes: 13 additions & 0 deletions numpy/core/tests/test_records.py
Expand Up @@ -356,6 +356,19 @@ def test_nonwriteable_setfield(self):
with assert_raises(ValueError):
r.setfield([2,3], *r.dtype.fields['f'])

def test_out_of_order_fields(self):
# names in the same order, padding added to descr
x = self.data[['col1', 'col2']]
assert_equal(x.dtype.names, ('col1', 'col2'))
assert_equal(x.dtype.descr,
[('col1', '<i4'), ('col2', '<i4'), ('', '|V4')])

# names change order to match indexing, as of 1.14 - descr can't
# represent that
y = self.data[['col2', 'col1']]
assert_equal(y.dtype.names, ('col2', 'col1'))
assert_raises(ValueError, lambda: y.dtype.descr)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests aren't valid in 1.14.1. I would replace the entire function with

dt = dtype({'names': ['a', 'b'], 
            'formats': ['i4', 'i4'], 
            'offsets': [4, 0]})
y = np.rec.fromrecords([(1, 2), (4, 5)], dtype=dt)
assert_raises(ValueError, lambda: y.dtype.descr)

def test_pickle_1(self):
# Issue #1529
a = np.array([(1, [])], dtype=[('a', np.int32), ('b', np.int32, 0)])
Expand Down