Skip to content

Commit 7e5dc82

Browse files
authored
Merge pull request numpy#27143 from seberg/issue-14142
BUG: Do not accidentally store dtype metadata in ``np.save``
2 parents 11eb606 + b9bcca0 commit 7e5dc82

File tree

3 files changed

+19
-19
lines changed

3 files changed

+19
-19
lines changed

numpy/lib/format.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,8 @@ def dtype_to_descr(dtype):
271271
warnings.warn("metadata on a dtype is not saved to an npy/npz. "
272272
"Use another format (such as pickle) to store it.",
273273
UserWarning, stacklevel=2)
274+
dtype = new_dtype
275+
274276
if dtype.names is not None:
275277
# This is a record array. The .descr is fine. XXX: parts of the
276278
# record array with an empty name, like padding bytes, still get

numpy/lib/tests/test_format.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -998,32 +998,30 @@ def test_header_growth_axis():
998998

999999
assert len(fp.getvalue()) == expected_header_length
10001000

1001-
@pytest.mark.parametrize('dt, fail', [
1002-
(np.dtype({'names': ['a', 'b'], 'formats': [float, np.dtype('S3',
1003-
metadata={'some': 'stuff'})]}), True),
1004-
(np.dtype(int, metadata={'some': 'stuff'}), False),
1005-
(np.dtype([('subarray', (int, (2,)))], metadata={'some': 'stuff'}), False),
1001+
@pytest.mark.parametrize('dt', [
1002+
np.dtype({'names': ['a', 'b'], 'formats': [float, np.dtype('S3',
1003+
metadata={'some': 'stuff'})]}),
1004+
np.dtype(int, metadata={'some': 'stuff'}),
1005+
np.dtype([('subarray', (int, (2,)))], metadata={'some': 'stuff'}),
10061006
# recursive: metadata on the field of a dtype
1007-
(np.dtype({'names': ['a', 'b'], 'formats': [
1007+
np.dtype({'names': ['a', 'b'], 'formats': [
10081008
float, np.dtype({'names': ['c'], 'formats': [np.dtype(int, metadata={})]})
1009-
]}), False)
1009+
]}),
10101010
])
10111011
@pytest.mark.skipif(IS_PYPY and sys.implementation.version <= (7, 3, 8),
10121012
reason="PyPy bug in error formatting")
1013-
def test_metadata_dtype(dt, fail):
1013+
def test_metadata_dtype(dt):
10141014
# gh-14142
10151015
arr = np.ones(10, dtype=dt)
10161016
buf = BytesIO()
10171017
with assert_warns(UserWarning):
10181018
np.save(buf, arr)
10191019
buf.seek(0)
1020-
if fail:
1021-
with assert_raises(ValueError):
1022-
np.load(buf)
1023-
else:
1024-
arr2 = np.load(buf)
1025-
# BUG: assert_array_equal does not check metadata
1026-
from numpy.lib._utils_impl import drop_metadata
1027-
assert_array_equal(arr, arr2)
1028-
assert drop_metadata(arr.dtype) is not arr.dtype
1029-
assert drop_metadata(arr2.dtype) is arr2.dtype
1020+
1021+
# Loading should work (metadata was stripped):
1022+
arr2 = np.load(buf)
1023+
# BUG: assert_array_equal does not check metadata
1024+
from numpy.lib._utils_impl import drop_metadata
1025+
assert_array_equal(arr, arr2)
1026+
assert drop_metadata(arr.dtype) is not arr.dtype
1027+
assert drop_metadata(arr2.dtype) is arr2.dtype

numpy/lib/tests/test_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def _compare_dtypes(dt1, dt2):
4343
assert dt_m.metadata is None
4444
assert dt_m['l1'].metadata is None
4545
assert dt_m['l1']['l2'].metadata is None
46-
46+
4747
# alignment
4848
dt = np.dtype([('x', '<f8'), ('y', '<i4')],
4949
align=True,

0 commit comments

Comments
 (0)