Skip to content

Commit ceb362a

Browse files
committed
Merge remote-tracking branch 'DanielNoord/typing/base'
* DanielNoord/typing/base: Add typing to ``_Error.__init__`` Add typing and check to ``_Error._matches_type`` Add typing to some methods of ``_Error``
2 parents 940150b + 7fd4d38 commit ceb362a

File tree

1 file changed

+33
-23
lines changed

1 file changed

+33
-23
lines changed

jsonschema/exceptions.py

+33-23
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from collections import defaultdict, deque
77
from pprint import pformat
88
from textwrap import dedent, indent
9-
from typing import TYPE_CHECKING, ClassVar
9+
from typing import TYPE_CHECKING, Any, ClassVar
1010
import heapq
1111
import itertools
1212
import warnings
@@ -19,6 +19,8 @@
1919
if TYPE_CHECKING:
2020
from collections.abc import Iterable, Mapping, MutableMapping
2121

22+
from jsonschema import _types
23+
2224
WEAK_MATCHES: frozenset[str] = frozenset(["anyOf", "oneOf"])
2325
STRONG_MATCHES: frozenset[str] = frozenset()
2426

@@ -44,17 +46,17 @@ class _Error(Exception):
4446
def __init__(
4547
self,
4648
message: str,
47-
validator=_unset,
48-
path=(),
49-
cause=None,
49+
validator: str | _utils.Unset = _unset,
50+
path: Iterable[str | int] = (),
51+
cause: Exception | None = None,
5052
context=(),
51-
validator_value=_unset,
52-
instance=_unset,
53-
schema=_unset,
54-
schema_path=(),
55-
parent=None,
56-
type_checker=_unset,
57-
):
53+
validator_value: Any = _unset,
54+
instance: Any = _unset,
55+
schema: Mapping[str, Any] | bool | _utils.Unset = _unset,
56+
schema_path: Iterable[str | int] = (),
57+
parent: _Error | None = None,
58+
type_checker: _types.TypeChecker | _utils.Unset = _unset,
59+
) -> None:
5860
super().__init__(
5961
message,
6062
validator,
@@ -82,10 +84,10 @@ def __init__(
8284
for error in context:
8385
error.parent = self
8486

85-
def __repr__(self):
87+
def __repr__(self) -> str:
8688
return f"<{self.__class__.__name__}: {self.message!r}>"
8789

88-
def __str__(self):
90+
def __str__(self) -> str:
8991
essential_for_verbose = (
9092
self.validator, self.validator_value, self.instance, self.schema,
9193
)
@@ -115,11 +117,11 @@ def __str__(self):
115117
)
116118

117119
@classmethod
118-
def create_from(cls, other):
120+
def create_from(cls, other: _Error):
119121
return cls(**other._contents())
120122

121123
@property
122-
def absolute_path(self):
124+
def absolute_path(self) -> deque[str | int]:
123125
parent = self.parent
124126
if parent is None:
125127
return self.relative_path
@@ -129,7 +131,7 @@ def absolute_path(self):
129131
return path
130132

131133
@property
132-
def absolute_schema_path(self):
134+
def absolute_schema_path(self) -> deque[str | int]:
133135
parent = self.parent
134136
if parent is None:
135137
return self.relative_schema_path
@@ -139,7 +141,7 @@ def absolute_schema_path(self):
139141
return path
140142

141143
@property
142-
def json_path(self):
144+
def json_path(self) -> str:
143145
path = "$"
144146
for elem in self.absolute_path:
145147
if isinstance(elem, int):
@@ -148,7 +150,11 @@ def json_path(self):
148150
path += "." + elem
149151
return path
150152

151-
def _set(self, type_checker=None, **kwargs):
153+
def _set(
154+
self,
155+
type_checker: _types.TypeChecker | None = None,
156+
**kwargs: Any,
157+
) -> None:
152158
if type_checker is not None and self._type_checker is _unset:
153159
self._type_checker = type_checker
154160

@@ -163,12 +169,16 @@ def _contents(self):
163169
)
164170
return {attr: getattr(self, attr) for attr in attrs}
165171

166-
def _matches_type(self):
172+
def _matches_type(self) -> bool:
167173
try:
168-
expected = self.schema["type"]
174+
# We ignore this as we want to simply crash if this happens
175+
expected = self.schema["type"] # type: ignore[index]
169176
except (KeyError, TypeError):
170177
return False
171178

179+
if isinstance(self._type_checker, _utils.Unset):
180+
return False
181+
172182
if isinstance(expected, str):
173183
return self._type_checker.is_type(self.instance, expected)
174184

@@ -215,7 +225,7 @@ def __eq__(self, other):
215225
return NotImplemented # pragma: no cover -- uncovered but deprecated # noqa: E501
216226
return self._cause == other._cause
217227

218-
def __str__(self):
228+
def __str__(self) -> str:
219229
return str(self._cause)
220230

221231

@@ -248,10 +258,10 @@ class UndefinedTypeCheck(Exception):
248258
A type checker was asked to check a type it did not have registered.
249259
"""
250260

251-
def __init__(self, type):
261+
def __init__(self, type: str) -> None:
252262
self.type = type
253263

254-
def __str__(self):
264+
def __str__(self) -> str:
255265
return f"Type {self.type!r} is unknown to this type checker"
256266

257267

0 commit comments

Comments
 (0)