Skip to content

Commit dfb65eb

Browse files
authored
Use ClassVar for _Error attributes
1 parent 19ed819 commit dfb65eb

File tree

1 file changed

+36
-51
lines changed

1 file changed

+36
-51
lines changed

jsonschema/exceptions.py

Lines changed: 36 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121

2222

2323
class _Error(Exception):
24+
25+
_word_for_schema_in_error_message: ClassVar[str]
26+
_word_for_instance_in_error_message: ClassVar[str]
27+
2428
def __init__(
2529
self,
2630
message: str,
@@ -65,8 +69,34 @@ def __init__(
6569
def __repr__(self):
6670
return f"<{self.__class__.__name__}: {self.message!r}>"
6771

68-
def __str__(self) -> str:
69-
return self.message
72+
def __str__(self):
73+
essential_for_verbose = (
74+
self.validator, self.validator_value, self.instance, self.schema,
75+
)
76+
if any(m is _unset for m in essential_for_verbose):
77+
return self.message
78+
79+
schema_path = _utils.format_as_index(
80+
container=self._word_for_schema_in_error_message,
81+
indices=list(self.relative_schema_path)[:-1],
82+
)
83+
instance_path = _utils.format_as_index(
84+
container=self._word_for_instance_in_error_message,
85+
indices=self.relative_path,
86+
)
87+
prefix = 16 * " "
88+
89+
return dedent(
90+
f"""\
91+
{self.message}
92+
93+
Failed validating {self.validator!r} in {schema_path}:
94+
{indent(pformat(self.schema, width=72), prefix).lstrip()}
95+
96+
On {instance_path}:
97+
{indent(pformat(self.instance, width=72), prefix).lstrip()}
98+
""".rstrip(),
99+
)
70100

71101
@classmethod
72102
def create_from(cls, other):
@@ -112,16 +142,8 @@ def _set(self, type_checker=None, **kwargs):
112142

113143
def _contents(self):
114144
attrs = (
115-
"message",
116-
"cause",
117-
"context",
118-
"validator",
119-
"validator_value",
120-
"path",
121-
"schema_path",
122-
"instance",
123-
"schema",
124-
"parent",
145+
"message", "cause", "context", "validator", "validator_value",
146+
"path", "schema_path", "instance", "schema", "parent",
125147
)
126148
return dict((attr, getattr(self, attr)) for attr in attrs)
127149

@@ -140,44 +162,7 @@ def _matches_type(self):
140162
)
141163

142164

143-
class _VerboseError(_Error):
144-
_word_for_schema_in_error_message: ClassVar[str]
145-
_word_for_instance_in_error_message: ClassVar[str]
146-
147-
def __str__(self):
148-
essential_for_verbose = (
149-
self.validator,
150-
self.validator_value,
151-
self.instance,
152-
self.schema,
153-
)
154-
if any(m is _unset for m in essential_for_verbose):
155-
return self.message
156-
157-
schema_path = _utils.format_as_index(
158-
container=self._word_for_schema_in_error_message,
159-
indices=list(self.relative_schema_path)[:-1],
160-
)
161-
instance_path = _utils.format_as_index(
162-
container=self._word_for_instance_in_error_message,
163-
indices=self.relative_path,
164-
)
165-
prefix = 16 * " "
166-
167-
return dedent(
168-
f"""\
169-
{self.message}
170-
171-
Failed validating {self.validator!r} in {schema_path}:
172-
{indent(pformat(self.schema, width=72), prefix).lstrip()}
173-
174-
On {instance_path}:
175-
{indent(pformat(self.instance, width=72), prefix).lstrip()}
176-
""".rstrip(),
177-
)
178-
179-
180-
class ValidationError(_VerboseError):
165+
class ValidationError(_Error):
181166
"""
182167
An instance was invalid under a provided schema.
183168
"""
@@ -186,7 +171,7 @@ class ValidationError(_VerboseError):
186171
_word_for_instance_in_error_message = "instance"
187172

188173

189-
class SchemaError(_VerboseError):
174+
class SchemaError(_Error):
190175
"""
191176
A schema was invalid under its corresponding metaschema.
192177
"""

0 commit comments

Comments
 (0)