diff --git a/docs/errors.rst b/docs/errors.rst index 5b0230f7e..78d9df502 100644 --- a/docs/errors.rst +++ b/docs/errors.rst @@ -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 diff --git a/jsonschema/exceptions.py b/jsonschema/exceptions.py index 80281057e..618007603 100644 --- a/jsonschema/exceptions.py +++ b/jsonschema/exceptions.py @@ -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 @@ -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] @@ -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(), ) @@ -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(), ) diff --git a/jsonschema/tests/test_exceptions.py b/jsonschema/tests/test_exceptions.py index 00ff30091..bf433be92 100644 --- a/jsonschema/tests/test_exceptions.py +++ b/jsonschema/tests/test_exceptions.py @@ -478,7 +478,9 @@ def test_empty_paths(self): self.assertShows( """ Failed validating 'type' in schema: - {'type': 'string'} + { + "type": "string" + } On instance: 5 @@ -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 @@ -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 @@ -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))),