Skip to content

Commit d610d7f

Browse files
committed
Remove custom __eq__ for value objects
This is a change of behaviour - see the removed test. However it is consistent with how value objects work in dataclasses / attrs
1 parent 5e47ffe commit d610d7f

2 files changed

Lines changed: 6 additions & 27 deletions

File tree

src/grimp/domain/valueobjects.py

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,14 @@
1-
from dataclasses import dataclass, astuple
2-
from typing import Set, Any
1+
from dataclasses import dataclass
2+
from typing import Set
33

44

5-
@dataclass(frozen=True, repr=False, eq=False)
5+
@dataclass(frozen=True, repr=False)
66
class ValueObject:
77
def __repr__(self) -> str:
88
return "<{}: {}>".format(self.__class__.__name__, self)
99

10-
# We use a custom __eq__ method to enforce the liskov principle.
11-
# e.g. SpecialModule("foo") == Module("foo") where SpecialModule is a subclass of Module.
12-
def __eq__(self, other: Any) -> bool:
13-
if isinstance(other, type(self)) or isinstance(self, type(other)):
14-
return hash(self) == hash(other)
15-
else:
16-
return False
17-
18-
def __hash__(self) -> int:
19-
return hash(astuple(self))
20-
2110

22-
@dataclass(frozen=True, repr=False, eq=False)
11+
@dataclass(frozen=True, repr=False)
2312
class Module(ValueObject):
2413
"""
2514
A Python module.
@@ -61,7 +50,7 @@ def is_descendant_of(self, module: "Module") -> bool:
6150
return self.name.startswith(f"{module.name}.")
6251

6352

64-
@dataclass(frozen=True, repr=False, eq=False)
53+
@dataclass(frozen=True, repr=False)
6554
class DirectImport(ValueObject):
6655
"""
6756
An import between one module and another.
@@ -76,7 +65,7 @@ def __str__(self) -> str:
7665
return f"{self.importer} -> {self.imported} (l. {self.line_number})"
7766

7867

79-
@dataclass(frozen=True, repr=False, eq=False)
68+
@dataclass(frozen=True, repr=False)
8069
class Layer(ValueObject):
8170
"""
8271
A layer within a layered architecture.

tests/unit/domain/test_valueobjects.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,6 @@ def test_equals(self):
1818
# Also non-Module instances should not be treated as equal.
1919
assert a != "foo"
2020

21-
def test_equals_obeys_liskov(self):
22-
class SpecialModule(Module):
23-
pass
24-
25-
module = Module("foo.bar")
26-
special_module = SpecialModule("foo.bar")
27-
28-
assert module == special_module
29-
assert special_module == module
30-
3121
def test_hash(self):
3222
a = Module("foo.bar")
3323
b = Module("foo.bar")

0 commit comments

Comments
 (0)