Skip to content

Commit ee024ff

Browse files
committed
Deprecate jsonschema.draftN_format_checker attributes.
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)
1 parent bd4306a commit ee024ff

File tree

5 files changed

+139
-24
lines changed

5 files changed

+139
-24
lines changed

CHANGELOG.rst

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ v4.16.0
33

44
* Improve the base URI behavior when resolving a ``$ref`` to a resolution URI
55
which is different from the resolved schema's declared ``$id``.
6+
* Accessing ``jsonschema.draftN_format_checker`` is deprecated. Instead, if you
7+
want access to the format checker itself, it is exposed as
8+
``jsonschema.validators.DraftNValidator.FORMAT_CHECKER`` on any
9+
``jsonschema.protocols.Validator``.
610

711
v4.15.0
812
=======

docs/validate.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ By default, no validation is enforced, but optionally, validation can be enabled
181181
>>> validate(
182182
... instance="-12",
183183
... schema={"format" : "ipv4"},
184-
... format_checker=draft202012_format_checker,
184+
... format_checker=Draft202012Validator.FORMAT_CHECKER,
185185
... )
186186
Traceback (most recent call last):
187187
...

jsonschema/__init__.py

+21-9
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,7 @@
1010
"""
1111
import warnings
1212

13-
from jsonschema._format import (
14-
FormatChecker,
15-
draft3_format_checker,
16-
draft4_format_checker,
17-
draft6_format_checker,
18-
draft7_format_checker,
19-
draft201909_format_checker,
20-
draft202012_format_checker,
21-
)
13+
from jsonschema._format import FormatChecker
2214
from jsonschema._types import TypeChecker
2315
from jsonschema.exceptions import (
2416
ErrorTree,
@@ -56,4 +48,24 @@ def __getattr__(name):
5648
import importlib_metadata as metadata
5749

5850
return metadata.version("jsonschema")
51+
52+
format_checkers = {
53+
"draft3_format_checker": Draft3Validator,
54+
"draft4_format_checker": Draft4Validator,
55+
"draft6_format_checker": Draft6Validator,
56+
"draft7_format_checker": Draft7Validator,
57+
"draft201909_format_checker": Draft201909Validator,
58+
"draft202012_format_checker": Draft202012Validator,
59+
}
60+
ValidatorForFormat = format_checkers.get(name)
61+
if ValidatorForFormat is not None:
62+
warnings.warn(
63+
f"Accessing jsonschema.{name} is deprecated and will be "
64+
"removed in a future release. Instead, use the FORMAT_CHECKER "
65+
"attribute on the corresponding Validator.",
66+
DeprecationWarning,
67+
stacklevel=2,
68+
)
69+
return ValidatorForFormat.FORMAT_CHECKER
70+
5971
raise AttributeError(f"module {__name__} has no attribute {name}")

jsonschema/tests/test_deprecations.py

+99
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,102 @@ def test_FormatChecker_cls_checks(self):
162162
self.assertTrue(
163163
str(w.warning).startswith("FormatChecker.cls_checks "),
164164
)
165+
166+
def test_draftN_format_checker(self):
167+
"""
168+
As of v4.16.0, accessing jsonschema.draftn_format_checker is deprecated
169+
in favor of Validator.FORMAT_CHECKER.
170+
"""
171+
172+
with self.assertWarns(DeprecationWarning) as w:
173+
from jsonschema import draft202012_format_checker # noqa
174+
175+
self.assertIs(
176+
draft202012_format_checker,
177+
validators.Draft202012Validator.FORMAT_CHECKER,
178+
)
179+
self.assertEqual(w.filename, __file__)
180+
self.assertTrue(
181+
str(w.warning).startswith(
182+
"Accessing jsonschema.draft202012_format_checker is ",
183+
),
184+
msg=w.warning,
185+
)
186+
187+
with self.assertWarns(DeprecationWarning) as w:
188+
from jsonschema import draft201909_format_checker # noqa
189+
190+
self.assertIs(
191+
draft201909_format_checker,
192+
validators.Draft201909Validator.FORMAT_CHECKER,
193+
)
194+
self.assertEqual(w.filename, __file__)
195+
self.assertTrue(
196+
str(w.warning).startswith(
197+
"Accessing jsonschema.draft201909_format_checker is ",
198+
),
199+
msg=w.warning,
200+
)
201+
202+
with self.assertWarns(DeprecationWarning) as w:
203+
from jsonschema import draft7_format_checker # noqa
204+
205+
self.assertIs(
206+
draft7_format_checker,
207+
validators.Draft7Validator.FORMAT_CHECKER,
208+
)
209+
self.assertEqual(w.filename, __file__)
210+
self.assertTrue(
211+
str(w.warning).startswith(
212+
"Accessing jsonschema.draft7_format_checker is ",
213+
),
214+
msg=w.warning,
215+
)
216+
217+
with self.assertWarns(DeprecationWarning) as w:
218+
from jsonschema import draft6_format_checker # noqa
219+
220+
self.assertIs(
221+
draft6_format_checker,
222+
validators.Draft6Validator.FORMAT_CHECKER,
223+
)
224+
self.assertEqual(w.filename, __file__)
225+
self.assertTrue(
226+
str(w.warning).startswith(
227+
"Accessing jsonschema.draft6_format_checker is ",
228+
),
229+
msg=w.warning,
230+
)
231+
232+
with self.assertWarns(DeprecationWarning) as w:
233+
from jsonschema import draft4_format_checker # noqa
234+
235+
self.assertIs(
236+
draft4_format_checker,
237+
validators.Draft4Validator.FORMAT_CHECKER,
238+
)
239+
self.assertEqual(w.filename, __file__)
240+
self.assertTrue(
241+
str(w.warning).startswith(
242+
"Accessing jsonschema.draft4_format_checker is ",
243+
),
244+
msg=w.warning,
245+
)
246+
247+
with self.assertWarns(DeprecationWarning) as w:
248+
from jsonschema import draft3_format_checker # noqa
249+
250+
self.assertIs(
251+
draft3_format_checker,
252+
validators.Draft3Validator.FORMAT_CHECKER,
253+
)
254+
self.assertEqual(w.filename, __file__)
255+
self.assertTrue(
256+
str(w.warning).startswith(
257+
"Accessing jsonschema.draft3_format_checker is ",
258+
),
259+
msg=w.warning,
260+
)
261+
262+
with self.assertRaises(ImportError):
263+
from jsonschema import draft1234_format_checker # noqa

jsonschema/tests/test_jsonschema_test_suite.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ def skipper(test):
2828
return skipper
2929

3030

31-
def missing_format(checker):
31+
def missing_format(Validator):
3232
def missing_format(test): # pragma: no cover
3333
schema = test.schema
3434
if (
3535
schema is True
3636
or schema is False
3737
or "format" not in schema
38-
or schema["format"] in checker.checkers
38+
or schema["format"] in Validator.FORMAT_CHECKER.checkers
3939
or test.valid
4040
):
4141
return
@@ -150,10 +150,10 @@ def leap_second(test):
150150
DRAFT3.optional_tests_of(name="non-bmp-regex"),
151151
DRAFT3.optional_tests_of(name="zeroTerminatedFloats"),
152152
Validator=jsonschema.Draft3Validator,
153-
format_checker=jsonschema.draft3_format_checker,
153+
format_checker=jsonschema.Draft3Validator.FORMAT_CHECKER,
154154
skip=lambda test: (
155155
narrow_unicode_build(test)
156-
or missing_format(jsonschema.draft3_format_checker)(test)
156+
or missing_format(jsonschema.Draft3Validator)(test)
157157
or complex_email_validation(test)
158158
or skip(
159159
message=bug(),
@@ -175,12 +175,12 @@ def leap_second(test):
175175
DRAFT4.optional_tests_of(name="non-bmp-regex"),
176176
DRAFT4.optional_tests_of(name="zeroTerminatedFloats"),
177177
Validator=jsonschema.Draft4Validator,
178-
format_checker=jsonschema.draft4_format_checker,
178+
format_checker=jsonschema.Draft4Validator.FORMAT_CHECKER,
179179
skip=lambda test: (
180180
narrow_unicode_build(test)
181181
or allowed_leading_zeros(test)
182182
or leap_second(test)
183-
or missing_format(jsonschema.draft4_format_checker)(test)
183+
or missing_format(jsonschema.Draft4Validator)(test)
184184
or complex_email_validation(test)
185185
or skip(
186186
message=bug(),
@@ -236,12 +236,12 @@ def leap_second(test):
236236
DRAFT6.optional_tests_of(name="float-overflow"),
237237
DRAFT6.optional_tests_of(name="non-bmp-regex"),
238238
Validator=jsonschema.Draft6Validator,
239-
format_checker=jsonschema.draft6_format_checker,
239+
format_checker=jsonschema.Draft6Validator.FORMAT_CHECKER,
240240
skip=lambda test: (
241241
narrow_unicode_build(test)
242242
or allowed_leading_zeros(test)
243243
or leap_second(test)
244-
or missing_format(jsonschema.draft6_format_checker)(test)
244+
or missing_format(jsonschema.Draft6Validator)(test)
245245
or complex_email_validation(test)
246246
or skip(
247247
message=bug(),
@@ -260,12 +260,12 @@ def leap_second(test):
260260
DRAFT7.optional_tests_of(name="float-overflow"),
261261
DRAFT7.optional_tests_of(name="non-bmp-regex"),
262262
Validator=jsonschema.Draft7Validator,
263-
format_checker=jsonschema.draft7_format_checker,
263+
format_checker=jsonschema.Draft7Validator.FORMAT_CHECKER,
264264
skip=lambda test: (
265265
narrow_unicode_build(test)
266266
or allowed_leading_zeros(test)
267267
or leap_second(test)
268-
or missing_format(jsonschema.draft7_format_checker)(test)
268+
or missing_format(jsonschema.Draft7Validator)(test)
269269
or complex_email_validation(test)
270270
or skip(
271271
message=bug(),
@@ -408,12 +408,12 @@ def leap_second(test):
408408
TestDraft201909Format = DRAFT201909.to_unittest_testcase(
409409
DRAFT201909.format_tests(),
410410
Validator=jsonschema.Draft201909Validator,
411-
format_checker=jsonschema.draft201909_format_checker,
411+
format_checker=jsonschema.Draft201909Validator.FORMAT_CHECKER,
412412
skip=lambda test: (
413413
complex_email_validation(test)
414414
or allowed_leading_zeros(test)
415415
or leap_second(test)
416-
or missing_format(jsonschema.draft201909_format_checker)(test)
416+
or missing_format(jsonschema.Draft201909Validator)(test)
417417
or complex_email_validation(test)
418418
),
419419
)
@@ -533,12 +533,12 @@ def leap_second(test):
533533
TestDraft202012Format = DRAFT202012.to_unittest_testcase(
534534
DRAFT202012.format_tests(),
535535
Validator=jsonschema.Draft202012Validator,
536-
format_checker=jsonschema.draft202012_format_checker,
536+
format_checker=jsonschema.Draft202012Validator.FORMAT_CHECKER,
537537
skip=lambda test: (
538538
complex_email_validation(test)
539539
or allowed_leading_zeros(test)
540540
or leap_second(test)
541-
or missing_format(jsonschema.draft202012_format_checker)(test)
541+
or missing_format(jsonschema.Draft202012Validator)(test)
542542
or complex_email_validation(test)
543543
),
544544
)

0 commit comments

Comments
 (0)