Skip to content

Commit

Permalink
Perform checks in method instead of returning a dictionary with checks
Browse files Browse the repository at this point in the history
  • Loading branch information
psomhorst committed Feb 2, 2024
1 parent 13c2384 commit 8509079
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
7 changes: 3 additions & 4 deletions eitprocessing/mixins/equality.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def _array_safe_eq(a, b) -> bool:
except TypeError:
return NotImplemented

def _isequivalent(
def isequivalent(
self,
other: Self,
raise_: bool = False,
Expand Down Expand Up @@ -67,9 +67,8 @@ def _isequivalent(
raise EquivalenceError(
f"Types don't match: {type(self)}, {type(other)}"
)
for msg, check in checks.items():
if not check:
raise EquivalenceError(msg)
if msg := self._perform_checks(other):
raise EquivalenceError(msg)
except EquivalenceError as e:
if raise_:
raise e
Expand Down
34 changes: 20 additions & 14 deletions eitprocessing/variants/variant_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,27 @@ def concatenate(cls, a: Self, b: Self) -> Self:

return obj

def isequivalent(
self,
other: Self,
raise_=False,
) -> bool:
# fmt: off
checks = {
f"Variant types don't match: {self.variant_type}, {other.variant_type}": self.variant_type == other.variant_type,
f"VariantCollections do not contain the same variants: {self.keys()=}, {other.keys()=}": set(self.keys()) == set(other.keys()),
}
def _perform_equivalence_checks(self, other) -> str | None:
if self.variant_type != other.variant_type:
return (
f"Variant types don't match: {self.variant_type}, {other.variant_type}"
)

if self.variant_type != other.variant_type:
return f"VariantCollections do not contain the same variants: {self.keys()=}, {other.keys()=}"

if set(self.keys()) != set(other.keys()):
return (
f"Variant types don't match: {self.variant_type}, {other.variant_type}"
)

for key in self.keys():
checks[f"Variant data ({key}) is not equivalent: {self[key]}, {other[key]}"] = \
Variant.isequivalent(self[key], other[key], raise_)
# fmt: on
return super()._isequivalent(other, raise_, checks)
if not Variant.isequivalent(self[key], other[key], False):
return (
f"Variant data ({key}) is not equivalent: {self[key]}, {other[key]}"
)

return None


class InvalidVariantType(TypeError):
Expand Down

0 comments on commit 8509079

Please sign in to comment.