@@ -176,226 +176,6 @@ def id_of(schema):
176
176
self .assertEqual (Derived .ID_OF (Derived .META_SCHEMA ), correct_id )
177
177
178
178
179
- class TestLegacyTypeChecking (SynchronousTestCase ):
180
- def test_create_default_types (self ):
181
- Validator = validators .create (meta_schema = {}, validators = ())
182
- self .assertEqual (
183
- set (Validator .DEFAULT_TYPES ), {
184
- u"array" ,
185
- u"boolean" ,
186
- u"integer" ,
187
- u"null" ,
188
- u"number" ,
189
- u"object" , u"string" ,
190
- },
191
- )
192
- self .flushWarnings ()
193
-
194
- def test_extend (self ):
195
- Validator = validators .create (meta_schema = {}, validators = ())
196
- original = dict (Validator .VALIDATORS )
197
- new = object ()
198
-
199
- Extended = validators .extend (
200
- Validator ,
201
- validators = {u"new" : new },
202
- )
203
- self .assertEqual (
204
- (
205
- Extended .VALIDATORS ,
206
- Extended .META_SCHEMA ,
207
- Extended .TYPE_CHECKER ,
208
- Validator .VALIDATORS ,
209
-
210
- Extended .DEFAULT_TYPES ,
211
- Extended ({}).DEFAULT_TYPES ,
212
- self .flushWarnings ()[0 ]["message" ],
213
- ), (
214
- dict (original , new = new ),
215
- Validator .META_SCHEMA ,
216
- Validator .TYPE_CHECKER ,
217
- original ,
218
-
219
- Validator .DEFAULT_TYPES ,
220
- Validator .DEFAULT_TYPES ,
221
- self .flushWarnings ()[0 ]["message" ],
222
- ),
223
- )
224
-
225
- def test_types_redefines_the_validators_type_checker (self ):
226
- schema = {"type" : "string" }
227
- self .assertFalse (validators .Draft7Validator (schema ).is_valid (12 ))
228
-
229
- validator = validators .Draft7Validator (
230
- schema ,
231
- types = {"string" : (str , int )},
232
- )
233
- self .assertTrue (validator .is_valid (12 ))
234
- self .flushWarnings ()
235
-
236
- def test_providing_default_types_warns (self ):
237
- self .assertWarns (
238
- category = DeprecationWarning ,
239
- message = (
240
- "The default_types argument is deprecated. "
241
- "Use the type_checker argument instead."
242
- ),
243
- # https://tm.tl/9363 :'(
244
- filename = sys .modules [self .assertWarns .__module__ ].__file__ ,
245
-
246
- f = validators .create ,
247
- meta_schema = {},
248
- validators = {},
249
- default_types = {"foo" : object },
250
- )
251
-
252
- def test_cannot_ask_for_default_types_with_non_default_type_checker (self ):
253
- """
254
- We raise an error when you ask a validator with non-default
255
- type checker for its DEFAULT_TYPES.
256
-
257
- The type checker argument is new, so no one but this library
258
- itself should be trying to use it, and doing so while then
259
- asking for DEFAULT_TYPES makes no sense (not to mention is
260
- deprecated), since type checkers are not strictly about Python
261
- type.
262
- """
263
- Validator = validators .create (
264
- meta_schema = {},
265
- validators = {},
266
- type_checker = TypeChecker (),
267
- )
268
- with self .assertRaises (validators ._DontDoThat ) as e :
269
- Validator .DEFAULT_TYPES
270
-
271
- self .assertIn (
272
- "DEFAULT_TYPES cannot be used on Validators using TypeCheckers" ,
273
- str (e .exception ),
274
- )
275
- with self .assertRaises (validators ._DontDoThat ):
276
- Validator ({}).DEFAULT_TYPES
277
-
278
- self .assertFalse (self .flushWarnings ())
279
-
280
- def test_providing_explicit_type_checker_does_not_warn (self ):
281
- Validator = validators .create (
282
- meta_schema = {},
283
- validators = {},
284
- type_checker = TypeChecker (),
285
- )
286
- self .assertFalse (self .flushWarnings ())
287
-
288
- Validator ({})
289
- self .assertFalse (self .flushWarnings ())
290
-
291
- def test_providing_neither_does_not_warn (self ):
292
- Validator = validators .create (meta_schema = {}, validators = {})
293
- self .assertFalse (self .flushWarnings ())
294
-
295
- Validator ({})
296
- self .assertFalse (self .flushWarnings ())
297
-
298
- def test_providing_default_types_with_type_checker_errors (self ):
299
- with self .assertRaises (TypeError ) as e :
300
- validators .create (
301
- meta_schema = {},
302
- validators = {},
303
- default_types = {"foo" : object },
304
- type_checker = TypeChecker (),
305
- )
306
-
307
- self .assertIn (
308
- "Do not specify default_types when providing a type checker" ,
309
- str (e .exception ),
310
- )
311
- self .assertFalse (self .flushWarnings ())
312
-
313
- def test_extending_a_legacy_validator_with_a_type_checker_errors (self ):
314
- Validator = validators .create (
315
- meta_schema = {},
316
- validators = {},
317
- default_types = {u"array" : list }
318
- )
319
- with self .assertRaises (TypeError ) as e :
320
- validators .extend (
321
- Validator ,
322
- validators = {},
323
- type_checker = TypeChecker (),
324
- )
325
-
326
- self .assertIn (
327
- (
328
- "Cannot extend a validator created with default_types "
329
- "with a type_checker. Update the validator to use a "
330
- "type_checker when created."
331
- ),
332
- str (e .exception ),
333
- )
334
- self .flushWarnings ()
335
-
336
- def test_extending_a_legacy_validator_does_not_rewarn (self ):
337
- Validator = validators .create (meta_schema = {}, default_types = {})
338
- self .assertTrue (self .flushWarnings ())
339
-
340
- validators .extend (Validator )
341
- self .assertFalse (self .flushWarnings ())
342
-
343
- def test_accessing_default_types_warns (self ):
344
- Validator = validators .create (meta_schema = {}, validators = {})
345
- self .assertFalse (self .flushWarnings ())
346
-
347
- self .assertWarns (
348
- DeprecationWarning ,
349
- (
350
- "The DEFAULT_TYPES attribute is deprecated. "
351
- "See the type checker attached to this validator instead."
352
- ),
353
- # https://tm.tl/9363 :'(
354
- sys .modules [self .assertWarns .__module__ ].__file__ ,
355
-
356
- getattr ,
357
- Validator ,
358
- "DEFAULT_TYPES" ,
359
- )
360
-
361
- def test_accessing_default_types_on_the_instance_warns (self ):
362
- Validator = validators .create (meta_schema = {}, validators = {})
363
- self .assertFalse (self .flushWarnings ())
364
-
365
- self .assertWarns (
366
- DeprecationWarning ,
367
- (
368
- "The DEFAULT_TYPES attribute is deprecated. "
369
- "See the type checker attached to this validator instead."
370
- ),
371
- # https://tm.tl/9363 :'(
372
- sys .modules [self .assertWarns .__module__ ].__file__ ,
373
-
374
- getattr ,
375
- Validator ({}),
376
- "DEFAULT_TYPES" ,
377
- )
378
-
379
- def test_providing_types_to_init_warns (self ):
380
- Validator = validators .create (meta_schema = {}, validators = {})
381
- self .assertFalse (self .flushWarnings ())
382
-
383
- self .assertWarns (
384
- category = DeprecationWarning ,
385
- message = (
386
- "The types argument is deprecated. "
387
- "Provide a type_checker to jsonschema.validators.extend "
388
- "instead."
389
- ),
390
- # https://tm.tl/9363 :'(
391
- filename = sys .modules [self .assertWarns .__module__ ].__file__ ,
392
-
393
- f = Validator ,
394
- schema = {},
395
- types = {"bar" : object },
396
- )
397
-
398
-
399
179
class TestIterErrors (TestCase ):
400
180
def setUp (self ):
401
181
self .validator = validators .Draft3Validator ({})
0 commit comments