diff options
author | Matti Picus <matti.picus@gmail.com> | 2019-12-02 20:08:45 +0200 |
---|---|---|
committer | Sebastian Berg <sebastian@sipsolutions.net> | 2019-12-02 12:08:45 -0600 |
commit | 7b2d968d5a4730489d9e9148afe2277b1bc32477 (patch) | |
tree | 3da8067d704a681cc48dfc372e3ed855d6cb1d0b /numpy/lib/format.py | |
parent | 48481c6e2ed958cd0d30cc0e8d5611c8e68f7df3 (diff) | |
download | numpy-7b2d968d5a4730489d9e9148afe2277b1bc32477.tar.gz |
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.
Diffstat (limited to 'numpy/lib/format.py')
-rw-r--r-- | numpy/lib/format.py | 14 |
1 files changed, 14 insertions, 0 deletions
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 |