Skip to content

Truncated pretty printing #749

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
40 changes: 23 additions & 17 deletions jsonschema/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"""
from collections import defaultdict, deque
import itertools
import pprint
import textwrap

import attr
import prettyprinter

from jsonschema import _utils

Expand All @@ -15,6 +15,12 @@

_unset = _utils.Unset()

prettyprinter.set_default_config(
max_seq_len=5,
width=80,
depth=3,
)


class _Error(Exception):
def __init__(
Expand Down Expand Up @@ -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
Expand Down
74 changes: 21 additions & 53 deletions jsonschema/tests/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -339,8 +339,7 @@ def test_empty_paths(self):
{'type': 'string'}

On instance:
5
""",
5""",
path=[],
schema_path=[],
)
Expand All @@ -352,8 +351,7 @@ def test_one_item_paths(self):
{'type': 'string'}

On instance[0]:
5
""",
5""",
path=[0],
schema_path=["items"],
)
Expand All @@ -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],
)
Expand All @@ -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",
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down