diff --git a/jsonschema/exceptions.py b/jsonschema/exceptions.py index ec9f5210e..c12d5177f 100644 --- a/jsonschema/exceptions.py +++ b/jsonschema/exceptions.py @@ -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) diff --git a/jsonschema/tests/test_exceptions.py b/jsonschema/tests/test_exceptions.py index 296fa2424..bbd562ac3 100644 --- a/jsonschema/tests/test_exceptions.py +++ b/jsonschema/tests/test_exceptions.py @@ -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)