Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

Commit f4c3490

Browse files
florimondmancalovelydinosaur
authored andcommitted
Make ValidationError hashable (#65)
* add failing test * update failing test * make Message and ValidationError hashable
1 parent 453c6fb commit f4c3490

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

tests/test_fields.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,3 +825,9 @@ def test_error_messages_interface():
825825
validator = Integer()
826826
value, error = validator.validate_or_error("abc")
827827
assert error.messages() == [Message(text="Must be a number.", code="type")]
828+
829+
830+
def test_validation_error_is_hashable():
831+
validator = Integer()
832+
_, error = validator.validate_or_error("abc")
833+
hash(error)

typesystem/base.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ def __eq__(self, other: typing.Any) -> bool:
7878
and self.end_position == other.end_position
7979
)
8080

81+
def __hash__(self) -> int:
82+
ident = (self.code, tuple(self.index))
83+
return hash(ident)
84+
8185
def __repr__(self) -> str:
8286
class_name = self.__class__.__name__
8387
index_str = f", index={self.index!r}" if self.index else ""
@@ -183,6 +187,10 @@ def __getitem__(self, key: typing.Any) -> typing.Union[str, dict]:
183187
def __eq__(self, other: typing.Any) -> bool:
184188
return isinstance(other, ValidationError) and self._messages == other._messages
185189

190+
def __hash__(self) -> int:
191+
ident = tuple(hash(m) for m in self._messages)
192+
return hash(ident)
193+
186194
def __repr__(self) -> str:
187195
class_name = self.__class__.__name__
188196
if len(self._messages) == 1 and not self._messages[0].index:

0 commit comments

Comments
 (0)