diff --git a/jsonschema/exceptions.py b/jsonschema/exceptions.py index 82d53da6..de66e9f9 100644 --- a/jsonschema/exceptions.py +++ b/jsonschema/exceptions.py @@ -6,7 +6,7 @@ from collections import defaultdict, deque from pprint import pformat from textwrap import dedent, indent -from typing import TYPE_CHECKING, ClassVar +from typing import TYPE_CHECKING, Any, ClassVar import heapq import itertools import warnings @@ -19,6 +19,8 @@ if TYPE_CHECKING: from collections.abc import Iterable, Mapping, MutableMapping + from jsonschema import _types + WEAK_MATCHES: frozenset[str] = frozenset(["anyOf", "oneOf"]) STRONG_MATCHES: frozenset[str] = frozenset() @@ -44,17 +46,17 @@ class _Error(Exception): def __init__( self, message: str, - validator=_unset, - path=(), - cause=None, + validator: str | _utils.Unset = _unset, + path: Iterable[str | int] = (), + cause: Exception | None = None, context=(), - validator_value=_unset, - instance=_unset, - schema=_unset, - schema_path=(), - parent=None, - type_checker=_unset, - ): + validator_value: Any = _unset, + instance: Any = _unset, + schema: Mapping[str, Any] | bool | _utils.Unset = _unset, + schema_path: Iterable[str | int] = (), + parent: _Error | None = None, + type_checker: _types.TypeChecker | _utils.Unset = _unset, + ) -> None: super().__init__( message, validator, @@ -82,10 +84,10 @@ def __init__( for error in context: error.parent = self - def __repr__(self): + def __repr__(self) -> str: return f"<{self.__class__.__name__}: {self.message!r}>" - def __str__(self): + def __str__(self) -> str: essential_for_verbose = ( self.validator, self.validator_value, self.instance, self.schema, ) @@ -115,11 +117,11 @@ def __str__(self): ) @classmethod - def create_from(cls, other): + def create_from(cls, other: _Error): return cls(**other._contents()) @property - def absolute_path(self): + def absolute_path(self) -> deque[str | int]: parent = self.parent if parent is None: return self.relative_path @@ -129,7 +131,7 @@ def absolute_path(self): return path @property - def absolute_schema_path(self): + def absolute_schema_path(self) -> deque[str | int]: parent = self.parent if parent is None: return self.relative_schema_path @@ -139,7 +141,7 @@ def absolute_schema_path(self): return path @property - def json_path(self): + def json_path(self) -> str: path = "$" for elem in self.absolute_path: if isinstance(elem, int): @@ -148,7 +150,11 @@ def json_path(self): path += "." + elem return path - def _set(self, type_checker=None, **kwargs): + def _set( + self, + type_checker: _types.TypeChecker | None = None, + **kwargs: Any, + ) -> None: if type_checker is not None and self._type_checker is _unset: self._type_checker = type_checker @@ -163,12 +169,16 @@ def _contents(self): ) return {attr: getattr(self, attr) for attr in attrs} - def _matches_type(self): + def _matches_type(self) -> bool: try: - expected = self.schema["type"] + # We ignore this as we want to simply crash if this happens + expected = self.schema["type"] # type: ignore[index] except (KeyError, TypeError): return False + if isinstance(self._type_checker, _utils.Unset): + return False + if isinstance(expected, str): return self._type_checker.is_type(self.instance, expected) @@ -215,7 +225,7 @@ def __eq__(self, other): return NotImplemented # pragma: no cover -- uncovered but deprecated # noqa: E501 return self._cause == other._cause - def __str__(self): + def __str__(self) -> str: return str(self._cause) @@ -248,10 +258,10 @@ class UndefinedTypeCheck(Exception): A type checker was asked to check a type it did not have registered. """ - def __init__(self, type): + def __init__(self, type: str) -> None: self.type = type - def __str__(self): + def __str__(self) -> str: return f"Type {self.type!r} is unknown to this type checker"