Skip to content
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
6 changes: 3 additions & 3 deletions jsonschema/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ def __str__(self):
return unicode(self).encode("utf-8")

def __unicode__(self):
if _unset in (
self.validator, self.validator_value, self.instance, self.schema,
):
if any(m is _unset for m in (
self.validator, self.validator_value, self.instance, self.schema
)):
return self.message

pschema = pprint.pformat(self.schema, width=72)
Expand Down
20 changes: 20 additions & 0 deletions jsonschema/tests/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,23 @@ def test_if_its_in_the_tree_anyhow_it_does_not_raise_an_error(self):
)
tree = exceptions.ErrorTree([error])
self.assertIsInstance(tree["foo"], exceptions.ErrorTree)


def test_str_works_with_instances_having_overriden_eq_operator(self):
"""
Checks for https://github.com/Julian/jsonschema/issues/164 which
rendered exceptions unusable when a `ValidationError` involved classes
withthe `eq` operator overridden (such as pandas.DataFrame),
caused by a `XX in YYY` check within `__unicode__`() method.
"""

class InstanceWithOverridenEq(object):
def __eq__(self, other):
raise Exception("Instance's __eq__()hould not have been called!")
inst = InstanceWithOverridenEq()
error = exceptions.ValidationError(
"a message", validator="foo", instance=inst, validator_value='some', schema='schema',
)

ex_str = str(error)
self.assertTrue(str(exceptions).find(type(inst).__name__), ex_str)