Skip to content

Commit a471d04

Browse files
committed
Merge remote-tracking branch 'ankostis/master'
* ankostis/master: Fixes #164, ValidationError.__unicode__(): use 'is' operator instead of '==' for unset-check Test-case for #164, ValidationError.__unicode__(): use 'is' operator instead of '==' for unset-check
2 parents 83e810c + 39499db commit a471d04

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

jsonschema/exceptions.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ def __str__(self):
4848
return unicode(self).encode("utf-8")
4949

5050
def __unicode__(self):
51-
if _unset in (
52-
self.validator, self.validator_value, self.instance, self.schema,
53-
):
51+
if any(m is _unset for m in (
52+
self.validator, self.validator_value, self.instance, self.schema
53+
)):
5454
return self.message
5555

5656
pschema = pprint.pformat(self.schema, width=72)

jsonschema/tests/test_exceptions.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,23 @@ def test_if_its_in_the_tree_anyhow_it_does_not_raise_an_error(self):
268268
)
269269
tree = exceptions.ErrorTree([error])
270270
self.assertIsInstance(tree["foo"], exceptions.ErrorTree)
271+
272+
273+
def test_str_works_with_instances_having_overriden_eq_operator(self):
274+
"""
275+
Checks for https://github.com/Julian/jsonschema/issues/164 which
276+
rendered exceptions unusable when a `ValidationError` involved classes
277+
withthe `eq` operator overridden (such as pandas.DataFrame),
278+
caused by a `XX in YYY` check within `__unicode__`() method.
279+
"""
280+
281+
class InstanceWithOverridenEq(object):
282+
def __eq__(self, other):
283+
raise Exception("Instance's __eq__()hould not have been called!")
284+
inst = InstanceWithOverridenEq()
285+
error = exceptions.ValidationError(
286+
"a message", validator="foo", instance=inst, validator_value='some', schema='schema',
287+
)
288+
289+
ex_str = str(error)
290+
self.assertTrue(str(exceptions).find(type(inst).__name__), ex_str)

0 commit comments

Comments
 (0)