Skip to content

Commit

Permalink
Make exceptions hashable (but not comparable)
Browse files Browse the repository at this point in the history
  • Loading branch information
stuarteberg committed Dec 6, 2018
1 parent 1a8ea88 commit 2c83d78
Show file tree
Hide file tree
Showing 15 changed files with 19 additions and 14 deletions.
Binary file added jsonschema/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file added jsonschema/__pycache__/_format.cpython-36.pyc
Binary file not shown.
Binary file added jsonschema/__pycache__/_types.cpython-36.pyc
Binary file not shown.
Binary file added jsonschema/__pycache__/_utils.cpython-36.pyc
Binary file not shown.
Binary file added jsonschema/__pycache__/_validators.cpython-36.pyc
Binary file not shown.
Binary file added jsonschema/__pycache__/_version.cpython-36.pyc
Binary file not shown.
Binary file added jsonschema/__pycache__/compat.cpython-36.pyc
Binary file not shown.
Binary file added jsonschema/__pycache__/exceptions.cpython-36.pyc
Binary file not shown.
Binary file added jsonschema/__pycache__/validators.cpython-36.pyc
Binary file not shown.
5 changes: 5 additions & 0 deletions jsonschema/_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

# This file is automatically generated by setup.py.
__version__ = '2.5.1.post64'
__sha__ = 'gcb20c89'
__revision__ = 'gcb20c89'
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
Binary file added jsonschema/tests/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file not shown.
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 2c83d78

Please sign in to comment.