summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian Berman <Julian@GrayVines.com>2021-08-20 11:11:55 +0100
committerJulian Berman <Julian@GrayVines.com>2021-08-20 11:11:55 +0100
commitb88852007bd365b40be64f1c9fe27a56dbc683ba (patch)
tree09c594d4b3b15fa9d1e16ca95736dc43860de73c
parent0b87431e8d90d765e6a0cb17527afac9f550f8b1 (diff)
downloadjsonschema-b88852007bd365b40be64f1c9fe27a56dbc683ba.tar.gz
Let format_index handle the container too.
May as well, this is only used in 2 places.
-rw-r--r--jsonschema/_utils.py12
-rw-r--r--jsonschema/exceptions.py14
2 files changed, 16 insertions, 10 deletions
diff --git a/jsonschema/_utils.py b/jsonschema/_utils.py
index f9fdd2c..47a1b32 100644
--- a/jsonschema/_utils.py
+++ b/jsonschema/_utils.py
@@ -71,22 +71,26 @@ def load_vocabulary(name):
return vocabulary
-def format_as_index(indices):
+def format_as_index(container, indices):
"""
Construct a single string containing indexing operations for the indices.
- For example, [1, 2, "foo"] -> [1][2]["foo"]
+ For example for a container ``bar``, [1, 2, "foo"] -> bar[1][2]["foo"]
Arguments:
+ container (str):
+
+ A word to use for the thing being indexed
+
indices (sequence):
The indices to format.
"""
if not indices:
- return ""
- return f"[{']['.join(repr(index) for index in indices)}]"
+ return container
+ return f"{container}[{']['.join(repr(index) for index in indices)}]"
def find_additional_properties(instance, schema):
diff --git a/jsonschema/exceptions.py b/jsonschema/exceptions.py
index 1fbb2ca..e2ec35f 100644
--- a/jsonschema/exceptions.py
+++ b/jsonschema/exceptions.py
@@ -66,22 +66,24 @@ class _Error(Exception):
if any(m is _unset for m in essential_for_verbose):
return self.message
- schema_word = self._word_for_schema_in_error_message
schema_path = _utils.format_as_index(
- list(self.relative_schema_path)[:-1],
+ container=self._word_for_schema_in_error_message,
+ indices=list(self.relative_schema_path)[:-1],
+ )
+ instance_path = _utils.format_as_index(
+ container=self._word_for_instance_in_error_message,
+ indices=self.relative_path,
)
- instance_word = self._word_for_instance_in_error_message
- instance_path = _utils.format_as_index(self.relative_path)
prefix = 16 * " "
return dedent(
f"""\
{self.message}
- Failed validating {self.validator!r} in {schema_word}{schema_path}:
+ Failed validating {self.validator!r} in {schema_path}:
{indent(pformat(self.schema, width=72), prefix).lstrip()}
- On {instance_word}{instance_path}:
+ On {instance_path}:
{indent(pformat(self.instance, width=72), prefix).lstrip()}
""".rstrip(),
)