Go's Set.ForEach(f func(c Cid) error) error stops iteration and returns the first error from the callback. Python's CIDSet.for_each(func) ignores return values and always returns None.
Problem
Go:
func (s *Set) ForEach(f func(c Cid) error) error {
for c := range s.set {
err := f(c)
if err != nil {
return err // ← Stops and returns error
}
}
return nil
}
Python:
def for_each(self, func):
for cid in self._set:
func(cid) # ← Return value ignored, always continues
Proposed Solution
Update for_each() to propagate errors:
def for_each(self, func):
"""Call func(cid) for each CID. Stop and return error if func raises or returns an error."""
for cid in self._set:
try:
result = func(cid)
if result is not None and isinstance(result, Exception):
return result
except Exception as e:
return e
return None
Add tests:
def test_cid_set_for_each_error_propagation():
cid_set = CIDSet()
cid_set.add(cidv0)
cid_set.add(cidv1)
def failing_func(cid):
raise ValueError("test error")
result = cid_set.for_each(failing_func)
assert isinstance(result, ValueError)
assert str(result) == "test error"
Related
Go's
Set.ForEach(f func(c Cid) error) errorstops iteration and returns the first error from the callback. Python'sCIDSet.for_each(func)ignores return values and always returnsNone.Problem
Go:
Python:
Proposed Solution
Update
for_each()to propagate errors:Add tests:
Related
set.goForEach()