summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian Berman <Julian@GrayVines.com>2022-09-09 11:55:56 +0300
committerJulian Berman <Julian@GrayVines.com>2022-09-09 11:55:56 +0300
commitee024ffc61bee56b25e816f489d7365fe1445c6d (patch)
tree3e2bb6cde1aec4cd489f018fbc46c354b5accc7b
parentbd4306a00ffecc715567d3117ce0ecc949f5dcc0 (diff)
downloadjsonschema-ee024ffc61bee56b25e816f489d7365fe1445c6d.tar.gz
Deprecate jsonschema.draftN_format_checker attributes.v4.16.0
Format support / enablement will change with vocabulary support, but in the interim, now that #905 is merged, these objects are better accessed by retrieving them directly from the corresponding validator. In other words: don't do: from jsonschema import draft202012_format_checker just do from jsonschema import Draft202012Validator do_whatever_with(Draft202012Validator.FORMAT_CHECKER)
-rw-r--r--CHANGELOG.rst4
-rw-r--r--docs/validate.rst2
-rw-r--r--jsonschema/__init__.py30
-rw-r--r--jsonschema/tests/test_deprecations.py99
-rw-r--r--jsonschema/tests/test_jsonschema_test_suite.py28
5 files changed, 139 insertions, 24 deletions
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index 4c886cf..a64a200 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -3,6 +3,10 @@ v4.16.0
* Improve the base URI behavior when resolving a ``$ref`` to a resolution URI
which is different from the resolved schema's declared ``$id``.
+* Accessing ``jsonschema.draftN_format_checker`` is deprecated. Instead, if you
+ want access to the format checker itself, it is exposed as
+ ``jsonschema.validators.DraftNValidator.FORMAT_CHECKER`` on any
+ ``jsonschema.protocols.Validator``.
v4.15.0
=======
diff --git a/docs/validate.rst b/docs/validate.rst
index f949837..9f88259 100644
--- a/docs/validate.rst
+++ b/docs/validate.rst
@@ -181,7 +181,7 @@ By default, no validation is enforced, but optionally, validation can be enabled
>>> validate(
... instance="-12",
... schema={"format" : "ipv4"},
- ... format_checker=draft202012_format_checker,
+ ... format_checker=Draft202012Validator.FORMAT_CHECKER,
... )
Traceback (most recent call last):
...
diff --git a/jsonschema/__init__.py b/jsonschema/__init__.py
index 7e3b91e..6628fc7 100644
--- a/jsonschema/__init__.py
+++ b/jsonschema/__init__.py
@@ -10,15 +10,7 @@ for you.
"""
import warnings
-from jsonschema._format import (
- FormatChecker,
- draft3_format_checker,
- draft4_format_checker,
- draft6_format_checker,
- draft7_format_checker,
- draft201909_format_checker,
- draft202012_format_checker,
-)
+from jsonschema._format import FormatChecker
from jsonschema._types import TypeChecker
from jsonschema.exceptions import (
ErrorTree,
@@ -56,4 +48,24 @@ def __getattr__(name):
import importlib_metadata as metadata
return metadata.version("jsonschema")
+
+ format_checkers = {
+ "draft3_format_checker": Draft3Validator,
+ "draft4_format_checker": Draft4Validator,
+ "draft6_format_checker": Draft6Validator,
+ "draft7_format_checker": Draft7Validator,
+ "draft201909_format_checker": Draft201909Validator,
+ "draft202012_format_checker": Draft202012Validator,
+ }
+ ValidatorForFormat = format_checkers.get(name)
+ if ValidatorForFormat is not None:
+ warnings.warn(
+ f"Accessing jsonschema.{name} is deprecated and will be "
+ "removed in a future release. Instead, use the FORMAT_CHECKER "
+ "attribute on the corresponding Validator.",
+ DeprecationWarning,
+ stacklevel=2,
+ )
+ return ValidatorForFormat.FORMAT_CHECKER
+
raise AttributeError(f"module {__name__} has no attribute {name}")
diff --git a/jsonschema/tests/test_deprecations.py b/jsonschema/tests/test_deprecations.py
index afa2658..54e2267 100644
--- a/jsonschema/tests/test_deprecations.py
+++ b/jsonschema/tests/test_deprecations.py
@@ -162,3 +162,102 @@ class TestDeprecations(TestCase):
self.assertTrue(
str(w.warning).startswith("FormatChecker.cls_checks "),
)
+
+ def test_draftN_format_checker(self):
+ """
+ As of v4.16.0, accessing jsonschema.draftn_format_checker is deprecated
+ in favor of Validator.FORMAT_CHECKER.
+ """
+
+ with self.assertWarns(DeprecationWarning) as w:
+ from jsonschema import draft202012_format_checker # noqa
+
+ self.assertIs(
+ draft202012_format_checker,
+ validators.Draft202012Validator.FORMAT_CHECKER,
+ )
+ self.assertEqual(w.filename, __file__)
+ self.assertTrue(
+ str(w.warning).startswith(
+ "Accessing jsonschema.draft202012_format_checker is ",
+ ),
+ msg=w.warning,
+ )
+
+ with self.assertWarns(DeprecationWarning) as w:
+ from jsonschema import draft201909_format_checker # noqa
+
+ self.assertIs(
+ draft201909_format_checker,
+ validators.Draft201909Validator.FORMAT_CHECKER,
+ )
+ self.assertEqual(w.filename, __file__)
+ self.assertTrue(
+ str(w.warning).startswith(
+ "Accessing jsonschema.draft201909_format_checker is ",
+ ),
+ msg=w.warning,
+ )
+
+ with self.assertWarns(DeprecationWarning) as w:
+ from jsonschema import draft7_format_checker # noqa
+
+ self.assertIs(
+ draft7_format_checker,
+ validators.Draft7Validator.FORMAT_CHECKER,
+ )
+ self.assertEqual(w.filename, __file__)
+ self.assertTrue(
+ str(w.warning).startswith(
+ "Accessing jsonschema.draft7_format_checker is ",
+ ),
+ msg=w.warning,
+ )
+
+ with self.assertWarns(DeprecationWarning) as w:
+ from jsonschema import draft6_format_checker # noqa
+
+ self.assertIs(
+ draft6_format_checker,
+ validators.Draft6Validator.FORMAT_CHECKER,
+ )
+ self.assertEqual(w.filename, __file__)
+ self.assertTrue(
+ str(w.warning).startswith(
+ "Accessing jsonschema.draft6_format_checker is ",
+ ),
+ msg=w.warning,
+ )
+
+ with self.assertWarns(DeprecationWarning) as w:
+ from jsonschema import draft4_format_checker # noqa
+
+ self.assertIs(
+ draft4_format_checker,
+ validators.Draft4Validator.FORMAT_CHECKER,
+ )
+ self.assertEqual(w.filename, __file__)
+ self.assertTrue(
+ str(w.warning).startswith(
+ "Accessing jsonschema.draft4_format_checker is ",
+ ),
+ msg=w.warning,
+ )
+
+ with self.assertWarns(DeprecationWarning) as w:
+ from jsonschema import draft3_format_checker # noqa
+
+ self.assertIs(
+ draft3_format_checker,
+ validators.Draft3Validator.FORMAT_CHECKER,
+ )
+ self.assertEqual(w.filename, __file__)
+ self.assertTrue(
+ str(w.warning).startswith(
+ "Accessing jsonschema.draft3_format_checker is ",
+ ),
+ msg=w.warning,
+ )
+
+ with self.assertRaises(ImportError):
+ from jsonschema import draft1234_format_checker # noqa
diff --git a/jsonschema/tests/test_jsonschema_test_suite.py b/jsonschema/tests/test_jsonschema_test_suite.py
index 43debbb..1e58365 100644
--- a/jsonschema/tests/test_jsonschema_test_suite.py
+++ b/jsonschema/tests/test_jsonschema_test_suite.py
@@ -28,14 +28,14 @@ def skip(message, **kwargs):
return skipper
-def missing_format(checker):
+def missing_format(Validator):
def missing_format(test): # pragma: no cover
schema = test.schema
if (
schema is True
or schema is False
or "format" not in schema
- or schema["format"] in checker.checkers
+ or schema["format"] in Validator.FORMAT_CHECKER.checkers
or test.valid
):
return
@@ -150,10 +150,10 @@ TestDraft3 = DRAFT3.to_unittest_testcase(
DRAFT3.optional_tests_of(name="non-bmp-regex"),
DRAFT3.optional_tests_of(name="zeroTerminatedFloats"),
Validator=jsonschema.Draft3Validator,
- format_checker=jsonschema.draft3_format_checker,
+ format_checker=jsonschema.Draft3Validator.FORMAT_CHECKER,
skip=lambda test: (
narrow_unicode_build(test)
- or missing_format(jsonschema.draft3_format_checker)(test)
+ or missing_format(jsonschema.Draft3Validator)(test)
or complex_email_validation(test)
or skip(
message=bug(),
@@ -175,12 +175,12 @@ TestDraft4 = DRAFT4.to_unittest_testcase(
DRAFT4.optional_tests_of(name="non-bmp-regex"),
DRAFT4.optional_tests_of(name="zeroTerminatedFloats"),
Validator=jsonschema.Draft4Validator,
- format_checker=jsonschema.draft4_format_checker,
+ format_checker=jsonschema.Draft4Validator.FORMAT_CHECKER,
skip=lambda test: (
narrow_unicode_build(test)
or allowed_leading_zeros(test)
or leap_second(test)
- or missing_format(jsonschema.draft4_format_checker)(test)
+ or missing_format(jsonschema.Draft4Validator)(test)
or complex_email_validation(test)
or skip(
message=bug(),
@@ -236,12 +236,12 @@ TestDraft6 = DRAFT6.to_unittest_testcase(
DRAFT6.optional_tests_of(name="float-overflow"),
DRAFT6.optional_tests_of(name="non-bmp-regex"),
Validator=jsonschema.Draft6Validator,
- format_checker=jsonschema.draft6_format_checker,
+ format_checker=jsonschema.Draft6Validator.FORMAT_CHECKER,
skip=lambda test: (
narrow_unicode_build(test)
or allowed_leading_zeros(test)
or leap_second(test)
- or missing_format(jsonschema.draft6_format_checker)(test)
+ or missing_format(jsonschema.Draft6Validator)(test)
or complex_email_validation(test)
or skip(
message=bug(),
@@ -260,12 +260,12 @@ TestDraft7 = DRAFT7.to_unittest_testcase(
DRAFT7.optional_tests_of(name="float-overflow"),
DRAFT7.optional_tests_of(name="non-bmp-regex"),
Validator=jsonschema.Draft7Validator,
- format_checker=jsonschema.draft7_format_checker,
+ format_checker=jsonschema.Draft7Validator.FORMAT_CHECKER,
skip=lambda test: (
narrow_unicode_build(test)
or allowed_leading_zeros(test)
or leap_second(test)
- or missing_format(jsonschema.draft7_format_checker)(test)
+ or missing_format(jsonschema.Draft7Validator)(test)
or complex_email_validation(test)
or skip(
message=bug(),
@@ -408,12 +408,12 @@ TestDraft201909 = DRAFT201909.to_unittest_testcase(
TestDraft201909Format = DRAFT201909.to_unittest_testcase(
DRAFT201909.format_tests(),
Validator=jsonschema.Draft201909Validator,
- format_checker=jsonschema.draft201909_format_checker,
+ format_checker=jsonschema.Draft201909Validator.FORMAT_CHECKER,
skip=lambda test: (
complex_email_validation(test)
or allowed_leading_zeros(test)
or leap_second(test)
- or missing_format(jsonschema.draft201909_format_checker)(test)
+ or missing_format(jsonschema.Draft201909Validator)(test)
or complex_email_validation(test)
),
)
@@ -533,12 +533,12 @@ TestDraft202012 = DRAFT202012.to_unittest_testcase(
TestDraft202012Format = DRAFT202012.to_unittest_testcase(
DRAFT202012.format_tests(),
Validator=jsonschema.Draft202012Validator,
- format_checker=jsonschema.draft202012_format_checker,
+ format_checker=jsonschema.Draft202012Validator.FORMAT_CHECKER,
skip=lambda test: (
complex_email_validation(test)
or allowed_leading_zeros(test)
or leap_second(test)
- or missing_format(jsonschema.draft202012_format_checker)(test)
+ or missing_format(jsonschema.Draft202012Validator)(test)
or complex_email_validation(test)
),
)