Skip to content

Commit

Permalink
Deprecate jsonschema.draftN_format_checker attributes.
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
Julian committed Sep 9, 2022
1 parent bd4306a commit ee024ff
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 24 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
=======
Expand Down
2 changes: 1 addition & 1 deletion docs/validate.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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):
...
Expand Down
30 changes: 21 additions & 9 deletions jsonschema/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,7 @@
"""
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,
Expand Down Expand Up @@ -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}")
99 changes: 99 additions & 0 deletions jsonschema/tests/test_deprecations.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,102 @@ def test_FormatChecker_cls_checks(self):
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
28 changes: 14 additions & 14 deletions jsonschema/tests/test_jsonschema_test_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ def skipper(test):
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
Expand Down Expand Up @@ -150,10 +150,10 @@ def leap_second(test):
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(),
Expand All @@ -175,12 +175,12 @@ def leap_second(test):
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(),
Expand Down Expand Up @@ -236,12 +236,12 @@ def leap_second(test):
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(),
Expand All @@ -260,12 +260,12 @@ def leap_second(test):
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(),
Expand Down Expand Up @@ -408,12 +408,12 @@ def leap_second(test):
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)
),
)
Expand Down Expand Up @@ -533,12 +533,12 @@ def leap_second(test):
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)
),
)

0 comments on commit ee024ff

Please sign in to comment.