From 1882597765cb4cb18ff7071df91d79be4c7ee0ac Mon Sep 17 00:00:00 2001 From: Atsuo Ishimoto Date: Thu, 27 Jul 2023 18:21:32 +0900 Subject: [PATCH 1/3] Use json.dumps() instead of pprint() --- jsonschema/exceptions.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/jsonschema/exceptions.py b/jsonschema/exceptions.py index 80281057e..47c9d7307 100644 --- a/jsonschema/exceptions.py +++ b/jsonschema/exceptions.py @@ -4,7 +4,7 @@ from __future__ import annotations from collections import defaultdict, deque -from pprint import pformat +import json from textwrap import dedent, indent from typing import ClassVar import heapq @@ -33,6 +33,9 @@ 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) + class _Error(Exception): _word_for_schema_in_error_message: ClassVar[str] @@ -104,10 +107,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 +271,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(), ) From 4ddeeb8348833328229b983f9c13190a84ca2d53 Mon Sep 17 00:00:00 2001 From: Atsuo Ishimoto Date: Fri, 28 Jul 2023 07:17:19 +0900 Subject: [PATCH 2/3] fix test --- jsonschema/exceptions.py | 5 +- jsonschema/tests/test_exceptions.py | 106 +++++++++++++++------------- 2 files changed, 61 insertions(+), 50 deletions(-) diff --git a/jsonschema/exceptions.py b/jsonschema/exceptions.py index 47c9d7307..618007603 100644 --- a/jsonschema/exceptions.py +++ b/jsonschema/exceptions.py @@ -4,11 +4,11 @@ from __future__ import annotations from collections import defaultdict, deque -import json from textwrap import dedent, indent from typing import ClassVar import heapq import itertools +import json import warnings from attrs import define @@ -34,7 +34,8 @@ def __getattr__(name): def _format_json(obj): - return json.dumps(obj, ensure_ascii=False, indent=2) + return json.dumps(obj, ensure_ascii=False, indent=2, default=repr, + sort_keys=True) class _Error(Exception): 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))), From 5bb76da171f07de657a67ff4c085cbe271835343 Mon Sep 17 00:00:00 2001 From: Atsuo Ishimoto Date: Fri, 28 Jul 2023 15:49:29 +0900 Subject: [PATCH 3/3] fix doc test --- docs/errors.rst | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) 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