6
6
from collections import defaultdict , deque
7
7
from pprint import pformat
8
8
from textwrap import dedent , indent
9
- from typing import TYPE_CHECKING , ClassVar
9
+ from typing import TYPE_CHECKING , Any , ClassVar
10
10
import heapq
11
11
import itertools
12
12
import warnings
19
19
if TYPE_CHECKING :
20
20
from collections .abc import Iterable , Mapping , MutableMapping
21
21
22
+ from jsonschema import _types
23
+
22
24
WEAK_MATCHES : frozenset [str ] = frozenset (["anyOf" , "oneOf" ])
23
25
STRONG_MATCHES : frozenset [str ] = frozenset ()
24
26
@@ -44,17 +46,17 @@ class _Error(Exception):
44
46
def __init__ (
45
47
self ,
46
48
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 ,
50
52
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 :
58
60
super ().__init__ (
59
61
message ,
60
62
validator ,
@@ -82,10 +84,10 @@ def __init__(
82
84
for error in context :
83
85
error .parent = self
84
86
85
- def __repr__ (self ):
87
+ def __repr__ (self ) -> str :
86
88
return f"<{ self .__class__ .__name__ } : { self .message !r} >"
87
89
88
- def __str__ (self ):
90
+ def __str__ (self ) -> str :
89
91
essential_for_verbose = (
90
92
self .validator , self .validator_value , self .instance , self .schema ,
91
93
)
@@ -115,11 +117,11 @@ def __str__(self):
115
117
)
116
118
117
119
@classmethod
118
- def create_from (cls , other ):
120
+ def create_from (cls , other : _Error ):
119
121
return cls (** other ._contents ())
120
122
121
123
@property
122
- def absolute_path (self ):
124
+ def absolute_path (self ) -> deque [ str | int ] :
123
125
parent = self .parent
124
126
if parent is None :
125
127
return self .relative_path
@@ -129,7 +131,7 @@ def absolute_path(self):
129
131
return path
130
132
131
133
@property
132
- def absolute_schema_path (self ):
134
+ def absolute_schema_path (self ) -> deque [ str | int ] :
133
135
parent = self .parent
134
136
if parent is None :
135
137
return self .relative_schema_path
@@ -139,7 +141,7 @@ def absolute_schema_path(self):
139
141
return path
140
142
141
143
@property
142
- def json_path (self ):
144
+ def json_path (self ) -> str :
143
145
path = "$"
144
146
for elem in self .absolute_path :
145
147
if isinstance (elem , int ):
@@ -148,7 +150,11 @@ def json_path(self):
148
150
path += "." + elem
149
151
return path
150
152
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 :
152
158
if type_checker is not None and self ._type_checker is _unset :
153
159
self ._type_checker = type_checker
154
160
@@ -163,12 +169,16 @@ def _contents(self):
163
169
)
164
170
return {attr : getattr (self , attr ) for attr in attrs }
165
171
166
- def _matches_type (self ):
172
+ def _matches_type (self ) -> bool :
167
173
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]
169
176
except (KeyError , TypeError ):
170
177
return False
171
178
179
+ if isinstance (self ._type_checker , _utils .Unset ):
180
+ return False
181
+
172
182
if isinstance (expected , str ):
173
183
return self ._type_checker .is_type (self .instance , expected )
174
184
@@ -215,7 +225,7 @@ def __eq__(self, other):
215
225
return NotImplemented # pragma: no cover -- uncovered but deprecated # noqa: E501
216
226
return self ._cause == other ._cause
217
227
218
- def __str__ (self ):
228
+ def __str__ (self ) -> str :
219
229
return str (self ._cause )
220
230
221
231
@@ -248,10 +258,10 @@ class UndefinedTypeCheck(Exception):
248
258
A type checker was asked to check a type it did not have registered.
249
259
"""
250
260
251
- def __init__ (self , type ) :
261
+ def __init__ (self , type : str ) -> None :
252
262
self .type = type
253
263
254
- def __str__ (self ):
264
+ def __str__ (self ) -> str :
255
265
return f"Type { self .type !r} is unknown to this type checker"
256
266
257
267
0 commit comments