Skip to content

Commit a97ad1b

Browse files
aulemahaldcherian
andauthored
Replace id comparison by value comparison in tests (#373)
* Relax check_unchanged to adapt to upstream changes * [test-upstream] Co-authored-by: dcherian <[email protected]>
1 parent 6994625 commit a97ad1b

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

cf_xarray/tests/test_accessor.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -938,10 +938,18 @@ def _make_names(prefixes):
938938

939939
def _check_unchanged(old, new):
940940
# Check data array attributes or global dataset attributes
941+
def _check_attrs_equal(o, n):
942+
# Compare values and keys,
943+
# But not ids : allow for copied values (see #365)
944+
assert o.keys() == n.keys()
945+
for k, v in o.items():
946+
if isinstance(v, np.ndarray):
947+
assert np.all(v == n[k])
948+
else:
949+
assert v == n[k]
950+
941951
assert type(old) == type(new)
942-
assert old.attrs.keys() == new.attrs.keys() # set comparison
943-
for att, old_val in old.attrs.items():
944-
assert id(old_val) == id(new.attrs[att])
952+
_check_attrs_equal(old.attrs, new.attrs)
945953

946954
# Check coordinate attributes and data variable attributes
947955
dicts = [(old.coords, new.coords)]
@@ -951,9 +959,7 @@ def _check_unchanged(old, new):
951959
assert old_dict.keys() == new_dict.keys() # set comparison
952960
for key, old_obj in old_dict.items():
953961
new_obj = new_dict[key]
954-
assert old_obj.attrs.keys() == new_obj.attrs.keys() # set comparison
955-
for att, old_val in old_obj.attrs.items():
956-
assert id(old_val) == id(new_obj.attrs[att]) # numpy-safe comparison
962+
_check_attrs_equal(old_obj.attrs, new_obj.attrs)
957963

958964

959965
_TIME_NAMES = ["t"] + _make_names(

0 commit comments

Comments
 (0)