Skip to content

Commit

Permalink
Enforce that NcAttribute.value is always an 0- or 1-D array. (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
pp-mo authored Jan 14, 2025
1 parent cb7a8f6 commit 7954c30
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
17 changes: 14 additions & 3 deletions lib/ncdata/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion lib/ncdata/utils/_compare_nc_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7954c30

Please sign in to comment.