From 456234a61f338b7cf7e528c5104d8f536fbc12dc Mon Sep 17 00:00:00 2001 From: "ankostis@host:STUW025" Date: Mon, 5 May 2014 19:04:35 +0200 Subject: [PATCH 1/2] Test-case for #164, ValidationError.__unicode__(): use 'is' operator instead of '==' for unset-check Currently test-case FAILS with a json-instances that have a screaming __eq__() operator. --- jsonschema/tests/test_exceptions.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) 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) From 39499db8ac6575575a17663d1c0b55d8f7df95a2 Mon Sep 17 00:00:00 2001 From: "ankostis@host:STUW025" Date: Mon, 5 May 2014 20:23:53 +0200 Subject: [PATCH 2/2] Fixes #164, ValidationError.__unicode__(): use 'is' operator instead of '==' for unset-check Test-case now passes OK. --- jsonschema/exceptions.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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)