Skip to content

Commit

Permalink
Make exceptions hashable (but not comparable)
Browse files Browse the repository at this point in the history
Fixes #477
  • Loading branch information
stuarteberg committed Dec 6, 2018
1 parent 1a8ea88 commit f9995aa
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
10 changes: 0 additions & 10 deletions jsonschema/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,6 @@ def __init__(
for error in context:
error.parent = self

def __eq__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
return self._contents() == other._contents()

def __ne__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
return not self == other

def __repr__(self):
return "<%s: %r>" % (self.__class__.__name__, self.message)

Expand Down
8 changes: 7 additions & 1 deletion jsonschema/tests/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def best_match(self, errors):
reversed_best = exceptions.best_match(reversed(errors))
msg = "Didn't return a consistent best match!\nGot: {0}\n\nThen: {1}"
self.assertEqual(
best, reversed_best, msg=msg.format(best, reversed_best),
best._contents(), reversed_best._contents(), msg=msg.format(best, reversed_best),
)
return best

Expand Down Expand Up @@ -460,3 +460,9 @@ def __ne__(this, other): # pragma: no cover
schema="schema",
)
self.assertIn(repr(instance), str(error))


class TestHashable(TestCase):
def test_hashable(self):
set([exceptions.ValidationError("")])
set([exceptions.SchemaError("")])
10 changes: 7 additions & 3 deletions jsonschema/tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,21 @@ def test_iter_errors(self):
schema = {u"startswith": u"hel"}
iter_errors = self.Validator(schema).iter_errors

self.assertEqual(list(iter_errors(u"hello")), [])
errors = list(iter_errors(u"hello"))
self.assertEqual(errors, [])

error = ValidationError(
expected_error = ValidationError(
u"Whoops!",
instance=u"goodbye",
schema=schema,
validator=u"startswith",
validator_value=u"hel",
schema_path=deque([u"startswith"]),
)
self.assertEqual(list(iter_errors(u"goodbye")), [error])

errors = list(iter_errors(u"goodbye"))
self.assertEqual(len(errors), 1)
self.assertEqual(errors[0]._contents(), expected_error._contents())

def test_if_a_version_is_provided_it_is_registered(self):
Validator = validators.create(
Expand Down

0 comments on commit f9995aa

Please sign in to comment.