summaryrefslogtreecommitdiff
path: root/jsonschema
diff options
context:
space:
mode:
authorDaniƫl van Noord <13665637+DanielNoord@users.noreply.github.com>2022-12-21 21:33:36 +0100
committerGitHub <noreply@github.com>2022-12-21 21:33:36 +0100
commitdfb65eb7199e6d187d198090746bd0494349f3d8 (patch)
treece78b9c0c385dd14fff2769bc9a457c9c8855e0e /jsonschema
parent19ed819626e413a0234f477fb0df0c668ad9d94e (diff)
downloadjsonschema-dfb65eb7199e6d187d198090746bd0494349f3d8.tar.gz
Use ``ClassVar`` for ``_Error`` attributes
Diffstat (limited to 'jsonschema')
-rw-r--r--jsonschema/exceptions.py87
1 files changed, 36 insertions, 51 deletions
diff --git a/jsonschema/exceptions.py b/jsonschema/exceptions.py
index 14f9a88..97a60ed 100644
--- a/jsonschema/exceptions.py
+++ b/jsonschema/exceptions.py
@@ -21,6 +21,10 @@ _unset = _utils.Unset()
class _Error(Exception):
+
+ _word_for_schema_in_error_message: ClassVar[str]
+ _word_for_instance_in_error_message: ClassVar[str]
+
def __init__(
self,
message: str,
@@ -65,8 +69,34 @@ class _Error(Exception):
def __repr__(self):
return f"<{self.__class__.__name__}: {self.message!r}>"
- def __str__(self) -> str:
- return self.message
+ def __str__(self):
+ essential_for_verbose = (
+ self.validator, self.validator_value, self.instance, self.schema,
+ )
+ if any(m is _unset for m in essential_for_verbose):
+ return self.message
+
+ schema_path = _utils.format_as_index(
+ 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,
+ )
+ prefix = 16 * " "
+
+ return dedent(
+ f"""\
+ {self.message}
+
+ Failed validating {self.validator!r} in {schema_path}:
+ {indent(pformat(self.schema, width=72), prefix).lstrip()}
+
+ On {instance_path}:
+ {indent(pformat(self.instance, width=72), prefix).lstrip()}
+ """.rstrip(),
+ )
@classmethod
def create_from(cls, other):
@@ -112,16 +142,8 @@ class _Error(Exception):
def _contents(self):
attrs = (
- "message",
- "cause",
- "context",
- "validator",
- "validator_value",
- "path",
- "schema_path",
- "instance",
- "schema",
- "parent",
+ "message", "cause", "context", "validator", "validator_value",
+ "path", "schema_path", "instance", "schema", "parent",
)
return dict((attr, getattr(self, attr)) for attr in attrs)
@@ -140,44 +162,7 @@ class _Error(Exception):
)
-class _VerboseError(_Error):
- _word_for_schema_in_error_message: ClassVar[str]
- _word_for_instance_in_error_message: ClassVar[str]
-
- def __str__(self):
- essential_for_verbose = (
- self.validator,
- self.validator_value,
- self.instance,
- self.schema,
- )
- if any(m is _unset for m in essential_for_verbose):
- return self.message
-
- schema_path = _utils.format_as_index(
- 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,
- )
- prefix = 16 * " "
-
- return dedent(
- f"""\
- {self.message}
-
- Failed validating {self.validator!r} in {schema_path}:
- {indent(pformat(self.schema, width=72), prefix).lstrip()}
-
- On {instance_path}:
- {indent(pformat(self.instance, width=72), prefix).lstrip()}
- """.rstrip(),
- )
-
-
-class ValidationError(_VerboseError):
+class ValidationError(_Error):
"""
An instance was invalid under a provided schema.
"""
@@ -186,7 +171,7 @@ class ValidationError(_VerboseError):
_word_for_instance_in_error_message = "instance"
-class SchemaError(_VerboseError):
+class SchemaError(_Error):
"""
A schema was invalid under its corresponding metaschema.
"""