1
+ from contextlib import suppress
1
2
from uuid import UUID
2
3
import datetime
3
4
import ipaddress
@@ -220,11 +221,9 @@ def is_ipv6(instance):
220
221
return not getattr (address , "scope_id" , "" )
221
222
222
223
223
- try :
224
+ with suppress ( ImportError ) :
224
225
from fqdn import FQDN
225
- except ImportError : # pragma: no cover
226
- pass
227
- else :
226
+
228
227
@_checks_drafts (
229
228
draft3 = "host-name" ,
230
229
draft4 = "hostname" ,
@@ -239,12 +238,10 @@ def is_host_name(instance):
239
238
return FQDN (instance ).is_valid
240
239
241
240
242
- try :
241
+ with suppress ( ImportError ) :
243
242
# The built-in `idna` codec only implements RFC 3890, so we go elsewhere.
244
243
import idna
245
- except ImportError : # pragma: no cover
246
- pass
247
- else :
244
+
248
245
@_checks_drafts (
249
246
draft7 = "idn-hostname" ,
250
247
draft201909 = "idn-hostname" ,
@@ -261,11 +258,9 @@ def is_idn_host_name(instance):
261
258
try :
262
259
import rfc3987
263
260
except ImportError :
264
- try :
261
+ with suppress ( ImportError ) :
265
262
from rfc3986_validator import validate_rfc3986
266
- except ImportError : # pragma: no cover
267
- pass
268
- else :
263
+
269
264
@_checks_drafts (name = "uri" )
270
265
def is_uri (instance ):
271
266
if not isinstance (instance , str ):
@@ -325,11 +320,9 @@ def is_uri_reference(instance):
325
320
return True
326
321
return rfc3987 .parse (instance , rule = "URI_reference" )
327
322
328
- try :
323
+ with suppress ( ImportError ) :
329
324
from rfc3339_validator import validate_rfc3339
330
- except ImportError :
331
- pass
332
- else :
325
+
333
326
@_checks_drafts (name = "date-time" )
334
327
def is_datetime (instance ):
335
328
if not isinstance (instance , str ):
@@ -374,16 +367,10 @@ def is_draft3_time(instance):
374
367
return datetime .datetime .strptime (instance , "%H:%M:%S" )
375
368
376
369
377
- try : # webcolors>=1.11
370
+ with suppress ( ImportError ):
378
371
from webcolors import CSS21_NAMES_TO_HEX
379
372
import webcolors
380
- except ImportError :
381
- try : # webcolors<1.11
382
- from webcolors import css21_names_to_hex as CSS21_NAMES_TO_HEX
383
- import webcolors
384
- except ImportError : # pragma: no cover
385
- pass
386
- else :
373
+
387
374
def is_css_color_code (instance ):
388
375
return webcolors .normalize_hex (instance )
389
376
@@ -402,11 +389,9 @@ def is_css3_color(instance):
402
389
return is_css_color_code (instance )
403
390
404
391
405
- try :
392
+ with suppress ( ImportError ) :
406
393
import jsonpointer
407
- except ImportError : # pragma: no cover
408
- pass
409
- else :
394
+
410
395
@_checks_drafts (
411
396
draft6 = "json-pointer" ,
412
397
draft7 = "json-pointer" ,
@@ -452,11 +437,9 @@ def is_relative_json_pointer(instance):
452
437
return (rest == "#" ) or jsonpointer .JsonPointer (rest )
453
438
454
439
455
- try :
440
+ with suppress ( ImportError ) :
456
441
import uri_template
457
- except ImportError : # pragma: no cover
458
- pass
459
- else :
442
+
460
443
@_checks_drafts (
461
444
draft6 = "uri-template" ,
462
445
draft7 = "uri-template" ,
@@ -469,11 +452,9 @@ def is_uri_template(instance):
469
452
return uri_template .validate (instance )
470
453
471
454
472
- try :
455
+ with suppress ( ImportError ) :
473
456
import isoduration
474
- except ImportError : # pragma: no cover
475
- pass
476
- else :
457
+
477
458
@_checks_drafts (
478
459
draft201909 = "duration" ,
479
460
draft202012 = "duration" ,
0 commit comments