Skip to content

Commit cd8f059

Browse files
committed
Deprecate FormatChecker.cls_checks.
It exposes fragile global state. Closes: #519.
1 parent b64cb5f commit cd8f059

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

CHANGELOG.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
v4.14.0
2+
=======
3+
4+
* ``FormatChecker.cls_checks`` is deprecated. Use ``FormatChecker.checks`` on
5+
an instance of ``FormatChecker`` instead.
6+
17
v4.13.0
28
=======
39

jsonschema/_format.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import ipaddress
77
import re
88
import typing
9+
import warnings
910

1011
from jsonschema.exceptions import FormatError
1112

@@ -84,6 +85,21 @@ def _checks(func: _F) -> _F:
8485
@classmethod
8586
def cls_checks(
8687
cls, format: str, raises: _RaisesType = (),
88+
) -> typing.Callable[[_F], _F]:
89+
warnings.warn(
90+
(
91+
"FormatChecker.cls_checks is deprecated. Call "
92+
"FormatChecker.checks on a specific FormatChecker instance "
93+
"instead."
94+
),
95+
DeprecationWarning,
96+
stacklevel=2,
97+
)
98+
return cls._cls_checks(format=format, raises=raises)
99+
100+
@classmethod
101+
def _cls_checks(
102+
cls, format: str, raises: _RaisesType = (),
87103
) -> typing.Callable[[_F], _F]:
88104
def _checks(func: _F) -> _F:
89105
cls.checkers[format] = (func, raises)
@@ -205,7 +221,7 @@ def wrap(func: _F) -> _F:
205221

206222
# Oy. This is bad global state, but relied upon for now, until
207223
# deprecation. See #519 and test_format_checkers_come_with_defaults
208-
FormatChecker.cls_checks(
224+
FormatChecker._cls_checks(
209225
draft202012 or draft201909 or draft7 or draft6 or draft4 or draft3,
210226
raises,
211227
)(func)

jsonschema/tests/test_deprecations.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from unittest import TestCase
22

3-
from jsonschema import validators
3+
from jsonschema import FormatChecker, validators
44

55

66
class TestDeprecations(TestCase):
@@ -146,3 +146,19 @@ class Subclass(validators.Draft202012Validator):
146146
with self.assertWarns(DeprecationWarning) as w:
147147
class AnotherSubclass(validators.create(meta_schema={})):
148148
pass
149+
150+
def test_FormatChecker_cls_checks(self):
151+
"""
152+
As of v4.14.0, FormatChecker.cls_checks is deprecated without
153+
replacement.
154+
"""
155+
156+
self.addCleanup(FormatChecker.checkers.pop, "boom", None)
157+
158+
with self.assertWarns(DeprecationWarning) as w:
159+
FormatChecker.cls_checks("boom")
160+
161+
self.assertEqual(w.filename, __file__)
162+
self.assertTrue(
163+
str(w.warning).startswith("FormatChecker.cls_checks "),
164+
)

jsonschema/tests/test_format.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ def test_it_raises_a_key_error_for_unknown_formats(self):
2929
def test_it_can_register_cls_checkers(self):
3030
original = dict(FormatChecker.checkers)
3131
self.addCleanup(FormatChecker.checkers.pop, "boom")
32-
FormatChecker.cls_checks("boom")(boom)
32+
with self.assertWarns(DeprecationWarning):
33+
FormatChecker.cls_checks("boom")(boom)
3334
self.assertEqual(
3435
FormatChecker.checkers,
3536
dict(original, boom=(boom, ())),

0 commit comments

Comments
 (0)