11
11
from jsonschema import ValidationError , _validators
12
12
from jsonschema ._types import TypeChecker
13
13
from jsonschema .exceptions import UndefinedTypeCheck , UnknownType
14
- from jsonschema .validators import Draft4Validator , extend
14
+ from jsonschema .validators import Draft202012Validator , extend
15
15
16
16
17
17
def equals_2 (checker , instance ):
@@ -23,7 +23,7 @@ def is_namedtuple(instance):
23
23
24
24
25
25
def is_object_or_named_tuple (checker , instance ):
26
- if Draft4Validator .TYPE_CHECKER .is_type (instance , "object" ):
26
+ if Draft202012Validator .TYPE_CHECKER .is_type (instance , "object" ):
27
27
return True
28
28
return is_namedtuple (instance )
29
29
@@ -40,11 +40,15 @@ def test_is_type(self):
40
40
)
41
41
42
42
def test_is_unknown_type (self ):
43
- with self .assertRaises (UndefinedTypeCheck ) as context :
43
+ with self .assertRaises (UndefinedTypeCheck ) as e :
44
44
TypeChecker ().is_type (4 , "foobar" )
45
45
self .assertIn (
46
46
"'foobar' is unknown to this type checker" ,
47
- str (context .exception ),
47
+ str (e .exception ),
48
+ )
49
+ self .assertTrue (
50
+ e .exception .__suppress_context__ ,
51
+ msg = "Expected the internal KeyError to be hidden." ,
48
52
)
49
53
50
54
def test_checks_can_be_added_at_init (self ):
@@ -114,8 +118,8 @@ def int_or_str_int(checker, instance):
114
118
return True
115
119
116
120
CustomValidator = extend (
117
- Draft4Validator ,
118
- type_checker = Draft4Validator .TYPE_CHECKER .redefine (
121
+ Draft202012Validator ,
122
+ type_checker = Draft202012Validator .TYPE_CHECKER .redefine (
119
123
"integer" , int_or_str_int ,
120
124
),
121
125
)
@@ -135,23 +139,29 @@ def test_object_can_be_extended(self):
135
139
136
140
Point = namedtuple ("Point" , ["x" , "y" ])
137
141
138
- type_checker = Draft4Validator .TYPE_CHECKER .redefine (
142
+ type_checker = Draft202012Validator .TYPE_CHECKER .redefine (
139
143
"object" , is_object_or_named_tuple ,
140
144
)
141
145
142
- CustomValidator = extend (Draft4Validator , type_checker = type_checker )
146
+ CustomValidator = extend (
147
+ Draft202012Validator ,
148
+ type_checker = type_checker ,
149
+ )
143
150
validator = CustomValidator (schema )
144
151
145
152
validator .validate (Point (x = 4 , y = 5 ))
146
153
147
154
def test_object_extensions_require_custom_validators (self ):
148
155
schema = {"type" : "object" , "required" : ["x" ]}
149
156
150
- type_checker = Draft4Validator .TYPE_CHECKER .redefine (
157
+ type_checker = Draft202012Validator .TYPE_CHECKER .redefine (
151
158
"object" , is_object_or_named_tuple ,
152
159
)
153
160
154
- CustomValidator = extend (Draft4Validator , type_checker = type_checker )
161
+ CustomValidator = extend (
162
+ Draft202012Validator ,
163
+ type_checker = type_checker ,
164
+ )
155
165
validator = CustomValidator (schema )
156
166
157
167
Point = namedtuple ("Point" , ["x" , "y" ])
@@ -166,7 +176,7 @@ def test_object_extensions_can_handle_custom_validators(self):
166
176
"properties" : {"x" : {"type" : "integer" }},
167
177
}
168
178
169
- type_checker = Draft4Validator .TYPE_CHECKER .redefine (
179
+ type_checker = Draft202012Validator .TYPE_CHECKER .redefine (
170
180
"object" , is_object_or_named_tuple ,
171
181
)
172
182
@@ -181,7 +191,7 @@ def coerced(validator, value, instance, schema):
181
191
properties = coerce_named_tuple (_validators .properties )
182
192
183
193
CustomValidator = extend (
184
- Draft4Validator ,
194
+ Draft202012Validator ,
185
195
type_checker = type_checker ,
186
196
validators = {"required" : required , "properties" : properties },
187
197
)
@@ -203,5 +213,5 @@ def coerced(validator, value, instance, schema):
203
213
204
214
def test_unknown_type (self ):
205
215
with self .assertRaises (UnknownType ) as e :
206
- Draft4Validator ({}).is_type (12 , "some unknown type" )
216
+ Draft202012Validator ({}).is_type (12 , "some unknown type" )
207
217
self .assertIn ("'some unknown type'" , str (e .exception ))
0 commit comments