Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/jsonschema/__init__.py: 46%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

37 statements  

1""" 

2An implementation of JSON Schema for Python. 

3 

4The main functionality is provided by the validator classes for each of the 

5supported JSON Schema versions. 

6 

7Most commonly, `jsonschema.validators.validate` is the quickest way to simply 

8validate a given instance under a schema, and will create a validator 

9for you. 

10""" 

11import warnings 

12 

13from jsonschema._format import FormatChecker 

14from jsonschema._types import TypeChecker 

15from jsonschema.exceptions import SchemaError, ValidationError 

16from jsonschema.validators import ( 

17 Draft3Validator, 

18 Draft4Validator, 

19 Draft6Validator, 

20 Draft7Validator, 

21 Draft201909Validator, 

22 Draft202012Validator, 

23 validate, 

24) 

25 

26 

27def __getattr__(name): 

28 if name == "__version__": 

29 warnings.warn( 

30 "Accessing jsonschema.__version__ is deprecated and will be " 

31 "removed in a future release. Use importlib.metadata directly " 

32 "to query for jsonschema's version.", 

33 DeprecationWarning, 

34 stacklevel=2, 

35 ) 

36 

37 from importlib import metadata 

38 return metadata.version("jsonschema") 

39 elif name == "RefResolver": 

40 from jsonschema.validators import _RefResolver 

41 warnings.warn( 

42 _RefResolver._DEPRECATION_MESSAGE, 

43 DeprecationWarning, 

44 stacklevel=2, 

45 ) 

46 return _RefResolver 

47 elif name == "ErrorTree": 

48 warnings.warn( 

49 "Importing ErrorTree directly from the jsonschema package " 

50 "is deprecated and will become an ImportError. Import it from " 

51 "jsonschema.exceptions instead.", 

52 DeprecationWarning, 

53 stacklevel=2, 

54 ) 

55 from jsonschema.exceptions import ErrorTree 

56 return ErrorTree 

57 elif name == "FormatError": 

58 warnings.warn( 

59 "Importing FormatError directly from the jsonschema package " 

60 "is deprecated and will become an ImportError. Import it from " 

61 "jsonschema.exceptions instead.", 

62 DeprecationWarning, 

63 stacklevel=2, 

64 ) 

65 from jsonschema.exceptions import FormatError 

66 return FormatError 

67 elif name == "Validator": 

68 warnings.warn( 

69 "Importing Validator directly from the jsonschema package " 

70 "is deprecated and will become an ImportError. Import it from " 

71 "jsonschema.protocols instead.", 

72 DeprecationWarning, 

73 stacklevel=2, 

74 ) 

75 from jsonschema.protocols import Validator 

76 return Validator 

77 elif name == "RefResolutionError": 

78 from jsonschema.exceptions import _RefResolutionError 

79 warnings.warn( 

80 _RefResolutionError._DEPRECATION_MESSAGE, 

81 DeprecationWarning, 

82 stacklevel=2, 

83 ) 

84 return _RefResolutionError 

85 

86 format_checkers = { 

87 "draft3_format_checker": Draft3Validator, 

88 "draft4_format_checker": Draft4Validator, 

89 "draft6_format_checker": Draft6Validator, 

90 "draft7_format_checker": Draft7Validator, 

91 "draft201909_format_checker": Draft201909Validator, 

92 "draft202012_format_checker": Draft202012Validator, 

93 } 

94 ValidatorForFormat = format_checkers.get(name) 

95 if ValidatorForFormat is not None: 

96 warnings.warn( 

97 f"Accessing jsonschema.{name} is deprecated and will be " 

98 "removed in a future release. Instead, use the FORMAT_CHECKER " 

99 "attribute on the corresponding Validator.", 

100 DeprecationWarning, 

101 stacklevel=2, 

102 ) 

103 return ValidatorForFormat.FORMAT_CHECKER 

104 

105 raise AttributeError(f"module {__name__} has no attribute {name}") 

106 

107 

108__all__ = [ 

109 "Draft201909Validator", 

110 "Draft202012Validator", 

111 "Draft3Validator", 

112 "Draft4Validator", 

113 "Draft6Validator", 

114 "Draft7Validator", 

115 "FormatChecker", 

116 "SchemaError", 

117 "TypeChecker", 

118 "ValidationError", 

119 "validate", 

120]