Skip to content

Commit 6f10ce6

Browse files
authored
Merge pull request #502 from stuarteberg/hashable-exceptions
Make exceptions hashable (but not comparable)
2 parents 8f8c85b + a7ca0c5 commit 6f10ce6

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

jsonschema/exceptions.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,6 @@ def __init__(
5555
for error in context:
5656
error.parent = self
5757

58-
def __eq__(self, other):
59-
if not isinstance(other, self.__class__):
60-
return NotImplemented
61-
return self._contents() == other._contents()
62-
63-
def __ne__(self, other):
64-
if not isinstance(other, self.__class__):
65-
return NotImplemented
66-
return not self == other
67-
6858
def __repr__(self):
6959
return "<%s: %r>" % (self.__class__.__name__, self.message)
7060

jsonschema/tests/test_exceptions.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ def best_match(self, errors):
1212
reversed_best = exceptions.best_match(reversed(errors))
1313
msg = "Didn't return a consistent best match!\nGot: {0}\n\nThen: {1}"
1414
self.assertEqual(
15-
best, reversed_best, msg=msg.format(best, reversed_best),
15+
best._contents(), reversed_best._contents(),
16+
msg=msg.format(best, reversed_best),
1617
)
1718
return best
1819

@@ -460,3 +461,9 @@ def __ne__(this, other): # pragma: no cover
460461
schema="schema",
461462
)
462463
self.assertIn(repr(instance), str(error))
464+
465+
466+
class TestHashable(TestCase):
467+
def test_hashable(self):
468+
set([exceptions.ValidationError("")])
469+
set([exceptions.SchemaError("")])

jsonschema/tests/test_validators.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,21 @@ def test_iter_errors(self):
7373
schema = {u"startswith": u"hel"}
7474
iter_errors = self.Validator(schema).iter_errors
7575

76-
self.assertEqual(list(iter_errors(u"hello")), [])
76+
errors = list(iter_errors(u"hello"))
77+
self.assertEqual(errors, [])
7778

78-
error = ValidationError(
79+
expected_error = ValidationError(
7980
u"Whoops!",
8081
instance=u"goodbye",
8182
schema=schema,
8283
validator=u"startswith",
8384
validator_value=u"hel",
8485
schema_path=deque([u"startswith"]),
8586
)
86-
self.assertEqual(list(iter_errors(u"goodbye")), [error])
87+
88+
errors = list(iter_errors(u"goodbye"))
89+
self.assertEqual(len(errors), 1)
90+
self.assertEqual(errors[0]._contents(), expected_error._contents())
8791

8892
def test_if_a_version_is_provided_it_is_registered(self):
8993
Validator = validators.create(

0 commit comments

Comments
 (0)