From f9995aa4e8aa9f9255d313771ed91d85730d6218 Mon Sep 17 00:00:00 2001 From: Stuart Berg Date: Wed, 5 Dec 2018 20:38:38 -0500 Subject: [PATCH] Make exceptions hashable (but not comparable) Fixes #477 --- jsonschema/exceptions.py | 10 ---------- jsonschema/tests/test_exceptions.py | 8 +++++++- jsonschema/tests/test_validators.py | 10 +++++++--- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/jsonschema/exceptions.py b/jsonschema/exceptions.py index f81fce097..90081821f 100644 --- a/jsonschema/exceptions.py +++ b/jsonschema/exceptions.py @@ -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) diff --git a/jsonschema/tests/test_exceptions.py b/jsonschema/tests/test_exceptions.py index fee638fe6..3c0d03e5f 100644 --- a/jsonschema/tests/test_exceptions.py +++ b/jsonschema/tests/test_exceptions.py @@ -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 @@ -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("")]) diff --git a/jsonschema/tests/test_validators.py b/jsonschema/tests/test_validators.py index 92b84ce3e..25cd43c66 100644 --- a/jsonschema/tests/test_validators.py +++ b/jsonschema/tests/test_validators.py @@ -73,9 +73,10 @@ 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, @@ -83,7 +84,10 @@ def test_iter_errors(self): 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(