Skip to content

Make exceptions hashable (but not comparable) #502

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
9 changes: 8 additions & 1 deletion jsonschema/tests/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ 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 +461,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