Skip to content

Faster pprint #1145

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
14 changes: 12 additions & 2 deletions docs/errors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,18 @@ easier debugging.
3 is not valid under any of the given schemas

Failed validating 'anyOf' in schema['items']:
{'anyOf': [{'maxLength': 2, 'type': 'string'},
{'minimum': 5, 'type': 'integer'}]}
{
"anyOf": [
{
"maxLength": 2,
"type": "string"
},
{
"minimum": 5,
"type": "integer"
}
]
}

On instance[1]:
3
Expand Down
14 changes: 9 additions & 5 deletions jsonschema/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
from __future__ import annotations

from collections import defaultdict, deque
from pprint import pformat
from textwrap import dedent, indent
from typing import ClassVar
import heapq
import itertools
import json
import warnings

from attrs import define
Expand All @@ -33,6 +33,10 @@ def __getattr__(name):
raise AttributeError(f"module {__name__} has no attribute {name}")


def _format_json(obj):
return json.dumps(obj, ensure_ascii=False, indent=2, default=repr,
sort_keys=True)

class _Error(Exception):

_word_for_schema_in_error_message: ClassVar[str]
Expand Down Expand Up @@ -104,10 +108,10 @@ def __str__(self):
{self.message}

Failed validating {self.validator!r} in {schema_path}:
{indent(pformat(self.schema, width=72), prefix).lstrip()}
{indent(_format_json(self.schema), prefix).lstrip()}

On {instance_path}:
{indent(pformat(self.instance, width=72), prefix).lstrip()}
{indent(_format_json(self.instance), prefix).lstrip()}
""".rstrip(),
)

Expand Down Expand Up @@ -268,10 +272,10 @@ def __str__(self):
return dedent(
f"""\
Unknown type {self.type!r} for validator with schema:
{indent(pformat(self.schema, width=72), prefix).lstrip()}
{indent(_format_json(self.schema), prefix).lstrip()}

While checking instance:
{indent(pformat(self.instance, width=72), prefix).lstrip()}
{indent(_format_json(self.instance), prefix).lstrip()}
""".rstrip(),
)

Expand Down
106 changes: 58 additions & 48 deletions jsonschema/tests/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,9 @@ def test_empty_paths(self):
self.assertShows(
"""
Failed validating 'type' in schema:
{'type': 'string'}
{
"type": "string"
}

On instance:
5
Expand All @@ -491,7 +493,9 @@ def test_one_item_paths(self):
self.assertShows(
"""
Failed validating 'type' in schema:
{'type': 'string'}
{
"type": "string"
}

On instance[0]:
5
Expand All @@ -504,7 +508,9 @@ def test_multiple_item_paths(self):
self.assertShows(
"""
Failed validating 'type' in schema['items'][0]:
{'type': 'string'}
{
"type": "string"
}

On instance[0]['a']:
5
Expand All @@ -517,53 +523,57 @@ 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,
"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
}

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,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24
]
""",
instance=list(range(25)),
schema=dict(zip(range(20), range(20))),
Expand Down