From 2f25a2cb7c54af3ec6fc68e8f79bd9d7bbc9d716 Mon Sep 17 00:00:00 2001 From: Patrick Peglar Date: Tue, 14 Jan 2025 11:41:41 +0000 Subject: [PATCH] Enforce that NcAttribute.value is always an 0- or 1-D array. --- lib/ncdata/_core.py | 17 ++++++++++++++--- lib/ncdata/utils/_compare_nc_datasets.py | 2 +- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/ncdata/_core.py b/lib/ncdata/_core.py index 909c589..1b687e4 100644 --- a/lib/ncdata/_core.py +++ b/lib/ncdata/_core.py @@ -503,12 +503,23 @@ def __init__(self, name: str, value): # noqa: D107 #: attribute name self.name: str = name # Attribute values are arraylike, have dtype - # TODO: may need to regularise string representations? - if not hasattr(value, "dtype"): - value = np.asanyarray(value) #: attribute value self.value: np.ndarray = value + @property + def value(self): # noqa: D102 + return self._value + + @value.setter + def value(self, value): + if not hasattr(value, "dtype"): + value = np.asanyarray(value) + if value.ndim > 1: + raise ValueError( + "Attribute value should only be 0- or 1-dimensional." + ) + self._value = value + def as_python_value(self): """ Return the content, but converting any character data to Python strings. diff --git a/lib/ncdata/utils/_compare_nc_datasets.py b/lib/ncdata/utils/_compare_nc_datasets.py index d2c6cca..655babf 100644 --- a/lib/ncdata/utils/_compare_nc_datasets.py +++ b/lib/ncdata/utils/_compare_nc_datasets.py @@ -385,7 +385,7 @@ def getdata(var): isnans, isnans2 = (np.isnan(arr) for arr in (flatdata, flatdata2)) if np.any(isnans) or np.any(isnans2): nandiffs = np.where(isnans != isnans2)[0] - if nandiffs: + if nandiffs.size > 0: flat_diff_inds += list(nandiffs) anynans = isnans | isnans2 flatdata[anynans] = safe_fill_const