Skip to content

Bug: C3dMapper.__eq__() returns early, skipping comparison of remaining keys #383

@yasumorishima

Description

@yasumorishima

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:

  1. Line 58 — When comparing C3dMapper children (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 c3d object with keys ["header", "parameters", "data"], only header is compared — parameters and data are never checked.

  2. Line 65 — When comparing np.ndarray values, return True is 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 False

I'd be happy to submit a PR with the fix and additional test coverage for __eq__() if you'd like.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions