diff --git a/jsonschema/exceptions.py b/jsonschema/exceptions.py index 46ffceda5..de4180e50 100644 --- a/jsonschema/exceptions.py +++ b/jsonschema/exceptions.py @@ -3,10 +3,10 @@ """ from collections import defaultdict, deque import itertools -import pprint import textwrap import attr +import prettyprinter from jsonschema import _utils @@ -15,6 +15,12 @@ _unset = _utils.Unset() +prettyprinter.set_default_config( + max_seq_len=5, + width=80, + depth=3, +) + class _Error(Exception): def __init__( @@ -66,24 +72,24 @@ def __str__(self): if any(m is _unset for m in essential_for_verbose): return self.message - pschema = pprint.pformat(self.schema, width=72) - pinstance = pprint.pformat(self.instance, width=72) - return self.message + textwrap.dedent(""" + pschema = prettyprinter.pformat(self.schema) + pinstance = prettyprinter.pformat(self.instance) - Failed validating %r in %s%s: - %s + return textwrap.dedent( + """{message} - On %s%s: - %s - """.rstrip() - ) % ( - self.validator, - self._word_for_schema_in_error_message, - _utils.format_as_index(list(self.relative_schema_path)[:-1]), - _utils.indent(pschema), - self._word_for_instance_in_error_message, - _utils.format_as_index(self.relative_path), - _utils.indent(pinstance), + Failed validating {validator!r} in {schema_word}{schema_path}:\n{schema} + + On {instance_word}{instance_path}:\n{instance}""" + ).format( + message=self.message, + validator=self.validator, + schema_word=self._word_for_schema_in_error_message, + schema_path=_utils.format_as_index(list(self.relative_schema_path)[:-1]), + schema=_utils.indent(pschema, 4), + instance_word=self._word_for_instance_in_error_message, + instance_path=_utils.format_as_index(self.relative_path), + instance=_utils.indent(pinstance, 4), ) @classmethod diff --git a/jsonschema/tests/test_exceptions.py b/jsonschema/tests/test_exceptions.py index a28555029..b80f03602 100644 --- a/jsonschema/tests/test_exceptions.py +++ b/jsonschema/tests/test_exceptions.py @@ -298,7 +298,7 @@ def make_error(self, **kwargs): return exceptions.ValidationError(**defaults) def assertShows(self, expected, **kwargs): - expected = textwrap.dedent(expected).rstrip("\n") + expected = expected.rstrip("\n") error = self.make_error(**kwargs) message_line, _, rest = str(error).partition("\n") @@ -339,8 +339,7 @@ def test_empty_paths(self): {'type': 'string'} On instance: - 5 - """, + 5""", path=[], schema_path=[], ) @@ -352,8 +351,7 @@ def test_one_item_paths(self): {'type': 'string'} On instance[0]: - 5 - """, + 5""", path=[0], schema_path=["items"], ) @@ -365,8 +363,7 @@ def test_multiple_item_paths(self): {'type': 'string'} On instance[0]['a']: - 5 - """, + 5""", path=[0, u"a"], schema_path=[u"items", 0, 1], ) @@ -375,54 +372,25 @@ def test_uses_pprint(self): self.assertShows( """ Failed validating 'maxLength' in schema: - {0: 0, - 1: 1, - 2: 2, - 3: 3, - 4: 4, - 5: 5, - 6: 6, - 7: 7, - 8: 8, - 9: 9, - 10: 10, - 11: 11, - 12: 12, - 13: 13, - 14: 14, - 15: 15, - 16: 16, - 17: 17, - 18: 18, - 19: 19} + { + 0: 0, + 1: 1, + 2: 2, + 3: 3, + 4: 4 + # ...and 15 more elements + } On instance: - [0, - 1, - 2, - 3, - 4, - 5, - 6, - 7, - 8, - 9, - 10, - 11, - 12, - 13, - 14, - 15, - 16, - 17, - 18, - 19, - 20, - 21, - 22, - 23, - 24] - """, + [ + 0, + 1, + 2, + 3, + 4, + # ...and 20 more elements + ]""", + instance=list(range(25)), schema=dict(zip(range(20), range(20))), validator=u"maxLength", diff --git a/setup.cfg b/setup.cfg index 6464c6ea4..0ee195645 100644 --- a/setup.cfg +++ b/setup.cfg @@ -29,6 +29,7 @@ install_requires = functools32;python_version<'3' importlib_metadata;python_version<'3.8' pyrsistent>=0.14.0 + prettyprinter [options.extras_require] format =