Skip to content

Commit 82cdede

Browse files
committed
refactor: changed __eq__ algo
1 parent c1a2f5a commit 82cdede

1 file changed

Lines changed: 12 additions & 22 deletions

File tree

src/inline_snapshot/matcher.py

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
from .pydantic_compatibility import PydanticUndefined
2-
from .pydantic_compatibility import get_fields
3-
4-
51
class IsPydanticAttributes:
62
def __init__(self, typ, /, **attributes):
73
self.typ = typ
@@ -20,32 +16,26 @@ def __eq__(self, other):
2016
):
2117
return NotImplemented
2218

19+
equal_attributes = {}
20+
2321
for name, attribute in self.attributes.items():
2422
if not hasattr(other, name):
2523
return False
26-
if not getattr(other, name) == attribute:
24+
other_attr = getattr(other, name)
25+
if not other_attr == attribute:
2726
return False
27+
equal_attributes[name] = other_attr
2828

29-
for name, field in get_fields(other).items(): # type: ignore
30-
if name in self.attributes:
31-
continue
32-
33-
if not getattr(field, "repr", True):
34-
continue
35-
36-
field_value = getattr(other, name)
37-
38-
if field.default is not PydanticUndefined and field.default == field_value:
39-
continue
29+
from pydantic import ValidationError
4030

41-
elif (
42-
field.default_factory is not None
43-
and field.default_factory() == field_value
44-
):
45-
continue
31+
try:
32+
new_other = self.typ(**equal_attributes)
33+
except ValidationError:
34+
# cases like missing non-default arguments
4635
return False
4736

48-
return True
37+
# check that all attributes which are not equal are equal to the default values
38+
return new_other == other
4939

5040
def __repr__(self):
5141
args = ", ".join(f"{k} = {v!r}" for k, v in self.attributes.items())

0 commit comments

Comments
 (0)