From 7b2d968d5a4730489d9e9148afe2277b1bc32477 Mon Sep 17 00:00:00 2001 From: Matti Picus Date: Mon, 2 Dec 2019 20:08:45 +0200 Subject: BUG: warn when saving dtype with metadata (#14994) Address gh-14142 for the 1.18 release: warn when saving a dtype with metadata that cannot be loaded. --- numpy/lib/format.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'numpy/lib/format.py') diff --git a/numpy/lib/format.py b/numpy/lib/format.py index 1ecd72815..20e2e9c72 100644 --- a/numpy/lib/format.py +++ b/numpy/lib/format.py @@ -242,6 +242,16 @@ def read_magic(fp): major, minor = magic_str[-2:] return major, minor +def _has_metadata(dt): + if dt.metadata is not None: + return True + elif dt.names is not None: + return any(_has_metadata(dt[k]) for k in dt.names) + elif dt.subdtype is not None: + return _has_metadata(dt.base) + else: + return False + def dtype_to_descr(dtype): """ Get a serializable descriptor from the dtype. @@ -265,6 +275,10 @@ def dtype_to_descr(dtype): replicate the input dtype. """ + if _has_metadata(dtype): + warnings.warn("metadata on a dtype may be saved or ignored, but will " + "raise if saved when read. Use another form of storage.", + UserWarning, stacklevel=2) if dtype.names is not None: # This is a record array. The .descr is fine. XXX: parts of the # record array with an empty name, like padding bytes, still get -- cgit v1.2.1