Skip to content

Commit 1dccf58

Browse files
authored
private coords to not affect equals (#586)
1 parent efca919 commit 1dccf58

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

dascore/core/coordmanager.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -779,10 +779,15 @@ def equals(self, other) -> bool:
779779
"""Return True if other coordinates are approx equal."""
780780
if not isinstance(other, self.__class__):
781781
return False
782-
if not (coord_set := set(self.coord_map)) == set(other.coord_map):
782+
if not set(self.dims) == set(other.dims):
783+
return False
784+
# Account for private coords (which don't have to be equal).
785+
s1 = {x for x in self.coord_map if x in self.dims or not x.startswith("_")}
786+
s2 = {x for x in other.coord_map if x in other.dims or not x.startswith("_")}
787+
if not s1 == s2:
783788
return False
784789
cdict_1, cdict_2 = self.coord_map, other.coord_map
785-
for name in coord_set:
790+
for name in s1:
786791
if not cdict_1[name].approx_equal(cdict_2[name]):
787792
return False
788793
return True

tests/test_core/test_coordmanager.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,24 @@ def test_cm_with_non_coord(self, cm_basic):
822822
assert non_2 != cm_basic
823823
assert cm_basic != non_2
824824

825+
def test_private_non_dimension_coord(self, cm_basic):
826+
"""Private, non-dimensional coords shouldn't affect comparisons."""
827+
time = cm_basic.get_array("time")
828+
new = cm_basic.update(_some_time=("time", time))
829+
assert new == cm_basic
830+
831+
def test_private_dims_effect_equals(self, cm_basic):
832+
"""Private dimensions should affect the comparisons."""
833+
time = cm_basic.get_array("time")
834+
new_time = time + dc.to_timedelta64(10)
835+
new = cm_basic.update(time=new_time)
836+
assert new != cm_basic
837+
838+
new_priv = new.rename_coord(time="_time")
839+
cm_priv = cm_basic.rename_coord(time="_time")
840+
assert "_time" in new_priv.dims
841+
assert new_priv != cm_priv
842+
825843

826844
class TestTranspose:
827845
"""Test suite for transposing dimensions."""

tests/test_utils/test_models.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ def test_private(self):
3232
mod2 = _TestModel(_private=2)
3333
assert sensible_model_equals(mod1, mod2)
3434

35+
def test_private_disjoint(self):
36+
"""Private attrs not shared should not affect equality."""
37+
mod1 = _TestModel(_private_1=1)
38+
mod2 = _TestModel(_private_2=2)
39+
assert sensible_model_equals(mod1, mod2)
40+
3541
def test_new(self):
3642
"""Ensure a new model can b e created."""
3743
mod = _TestModel(some_str="test")

0 commit comments

Comments
 (0)