-
Notifications
You must be signed in to change notification settings - Fork 54
Description
Hi @pariterre, thanks for this great library!
I noticed a potential bug in C3dMapper.__eq__() in binding/python3/__init__.py.
Problem
The for loop in __eq__() has two early return statements that cause the comparison to stop after checking only the first key, instead of iterating through all keys:
-
Line 58 — When comparing
C3dMapperchildren (e.g.,header,parameters,data), the method returns immediately after comparing the first one:if isinstance(self._storage[key], C3dMapper) and isinstance(other._storage[key], C3dMapper): return self._storage[key] == other._storage[key] # returns on first key
For a top-level
c3dobject with keys["header", "parameters", "data"], onlyheaderis compared —parametersanddataare never checked. -
Line 65 — When comparing
np.ndarrayvalues,return Trueis called after the first matching array:np.testing.assert_array_equal(self._storage[key], other._storage[key]) return True # returns without checking remaining arrays
Expected behavior
All keys should be compared before returning True. The dict branch (line 59-61) already does this correctly — it only returns False on mismatch and continues otherwise.
Suggested fix
# Line 58: only return on mismatch
if isinstance(self._storage[key], C3dMapper) and isinstance(other._storage[key], C3dMapper):
if not (self._storage[key] == other._storage[key]):
return False
# Line 62-67: remove return True, let the loop continue
elif isinstance(self._storage[key], np.ndarray) and isinstance(other._storage[key], np.ndarray):
if not np.array_equal(self._storage[key], other._storage[key]):
return FalseI'd be happy to submit a PR with the fix and additional test coverage for __eq__() if you'd like.