Skip to content

Commit b888520

Browse files
committed
Let format_index handle the container too.
May as well, this is only used in 2 places.
1 parent 0b87431 commit b888520

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

jsonschema/_utils.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,26 @@ def load_vocabulary(name):
7171
return vocabulary
7272

7373

74-
def format_as_index(indices):
74+
def format_as_index(container, indices):
7575
"""
7676
Construct a single string containing indexing operations for the indices.
7777
78-
For example, [1, 2, "foo"] -> [1][2]["foo"]
78+
For example for a container ``bar``, [1, 2, "foo"] -> bar[1][2]["foo"]
7979
8080
Arguments:
8181
82+
container (str):
83+
84+
A word to use for the thing being indexed
85+
8286
indices (sequence):
8387
8488
The indices to format.
8589
"""
8690

8791
if not indices:
88-
return ""
89-
return f"[{']['.join(repr(index) for index in indices)}]"
92+
return container
93+
return f"{container}[{']['.join(repr(index) for index in indices)}]"
9094

9195

9296
def find_additional_properties(instance, schema):

jsonschema/exceptions.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,24 @@ def __str__(self):
6666
if any(m is _unset for m in essential_for_verbose):
6767
return self.message
6868

69-
schema_word = self._word_for_schema_in_error_message
7069
schema_path = _utils.format_as_index(
71-
list(self.relative_schema_path)[:-1],
70+
container=self._word_for_schema_in_error_message,
71+
indices=list(self.relative_schema_path)[:-1],
72+
)
73+
instance_path = _utils.format_as_index(
74+
container=self._word_for_instance_in_error_message,
75+
indices=self.relative_path,
7276
)
73-
instance_word = self._word_for_instance_in_error_message
74-
instance_path = _utils.format_as_index(self.relative_path)
7577
prefix = 16 * " "
7678

7779
return dedent(
7880
f"""\
7981
{self.message}
8082
81-
Failed validating {self.validator!r} in {schema_word}{schema_path}:
83+
Failed validating {self.validator!r} in {schema_path}:
8284
{indent(pformat(self.schema, width=72), prefix).lstrip()}
8385
84-
On {instance_word}{instance_path}:
86+
On {instance_path}:
8587
{indent(pformat(self.instance, width=72), prefix).lstrip()}
8688
""".rstrip(),
8789
)

0 commit comments

Comments
 (0)