__init__.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. """
  2. An implementation of JSON Schema for Python.
  3. The main functionality is provided by the validator classes for each of the
  4. supported JSON Schema versions.
  5. Most commonly, `jsonschema.validators.validate` is the quickest way to simply
  6. validate a given instance under a schema, and will create a validator
  7. for you.
  8. """
  9. import warnings
  10. from jsonschema._format import FormatChecker
  11. from jsonschema._types import TypeChecker
  12. from jsonschema.exceptions import SchemaError, ValidationError
  13. from jsonschema.validators import (
  14. Draft3Validator,
  15. Draft4Validator,
  16. Draft6Validator,
  17. Draft7Validator,
  18. Draft201909Validator,
  19. Draft202012Validator,
  20. validate,
  21. )
  22. def __getattr__(name):
  23. if name == "__version__":
  24. warnings.warn(
  25. "Accessing jsonschema.__version__ is deprecated and will be "
  26. "removed in a future release. Use importlib.metadata directly "
  27. "to query for jsonschema's version.",
  28. DeprecationWarning,
  29. stacklevel=2,
  30. )
  31. from importlib import metadata
  32. return metadata.version("jsonschema")
  33. elif name == "RefResolver":
  34. from jsonschema.validators import _RefResolver
  35. warnings.warn(
  36. _RefResolver._DEPRECATION_MESSAGE,
  37. DeprecationWarning,
  38. stacklevel=2,
  39. )
  40. return _RefResolver
  41. elif name == "ErrorTree":
  42. warnings.warn(
  43. "Importing ErrorTree directly from the jsonschema package "
  44. "is deprecated and will become an ImportError. Import it from "
  45. "jsonschema.exceptions instead.",
  46. DeprecationWarning,
  47. stacklevel=2,
  48. )
  49. from jsonschema.exceptions import ErrorTree
  50. return ErrorTree
  51. elif name == "FormatError":
  52. warnings.warn(
  53. "Importing FormatError directly from the jsonschema package "
  54. "is deprecated and will become an ImportError. Import it from "
  55. "jsonschema.exceptions instead.",
  56. DeprecationWarning,
  57. stacklevel=2,
  58. )
  59. from jsonschema.exceptions import FormatError
  60. return FormatError
  61. elif name == "Validator":
  62. warnings.warn(
  63. "Importing Validator directly from the jsonschema package "
  64. "is deprecated and will become an ImportError. Import it from "
  65. "jsonschema.protocols instead.",
  66. DeprecationWarning,
  67. stacklevel=2,
  68. )
  69. from jsonschema.protocols import Validator
  70. return Validator
  71. elif name == "RefResolutionError":
  72. from jsonschema.exceptions import _RefResolutionError
  73. warnings.warn(
  74. _RefResolutionError._DEPRECATION_MESSAGE,
  75. DeprecationWarning,
  76. stacklevel=2,
  77. )
  78. return _RefResolutionError
  79. format_checkers = {
  80. "draft3_format_checker": Draft3Validator,
  81. "draft4_format_checker": Draft4Validator,
  82. "draft6_format_checker": Draft6Validator,
  83. "draft7_format_checker": Draft7Validator,
  84. "draft201909_format_checker": Draft201909Validator,
  85. "draft202012_format_checker": Draft202012Validator,
  86. }
  87. ValidatorForFormat = format_checkers.get(name)
  88. if ValidatorForFormat is not None:
  89. warnings.warn(
  90. f"Accessing jsonschema.{name} is deprecated and will be "
  91. "removed in a future release. Instead, use the FORMAT_CHECKER "
  92. "attribute on the corresponding Validator.",
  93. DeprecationWarning,
  94. stacklevel=2,
  95. )
  96. return ValidatorForFormat.FORMAT_CHECKER
  97. raise AttributeError(f"module {__name__} has no attribute {name}")
  98. __all__ = [
  99. "Draft201909Validator",
  100. "Draft202012Validator",
  101. "Draft3Validator",
  102. "Draft4Validator",
  103. "Draft6Validator",
  104. "Draft7Validator",
  105. "FormatChecker",
  106. "SchemaError",
  107. "TypeChecker",
  108. "ValidationError",
  109. "validate",
  110. ]