Skip to content

Commit eaf2e7b

Browse files
committed
Deprecate importing Validator from the package root.
Import it from jsonschema.protocols instead.
1 parent 8a55721 commit eaf2e7b

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

CHANGELOG.rst

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
v4.19.0
2+
=======
3+
4+
* Importing the ``Validator`` protocol directly from the package root is deprecated.
5+
Import it from ``jsonschema.protocols.Validator`` instead.
6+
17
v4.18.6
28
=======
39

jsonschema/__init__.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from jsonschema._format import FormatChecker
1414
from jsonschema._types import TypeChecker
1515
from jsonschema.exceptions import SchemaError, ValidationError
16-
from jsonschema.protocols import Validator
1716
from jsonschema.validators import (
1817
Draft3Validator,
1918
Draft4Validator,
@@ -65,6 +64,16 @@ def __getattr__(name):
6564
)
6665
from jsonschema.exceptions import FormatError
6766
return FormatError
67+
elif name == "Validator":
68+
warnings.warn(
69+
"Importing Validator directly from the jsonschema package "
70+
"is deprecated and will become an ImportError. Import it from "
71+
"jsonschema.protocols instead.",
72+
DeprecationWarning,
73+
stacklevel=2,
74+
)
75+
from jsonschema.protocols import Validator
76+
return Validator
6877
elif name == "RefResolutionError":
6978
from jsonschema.exceptions import _RefResolutionError
7079
warnings.warn(
@@ -107,6 +116,5 @@ def __getattr__(name):
107116
"SchemaError",
108117
"TypeChecker",
109118
"ValidationError",
110-
"Validator",
111119
"validate",
112120
]

jsonschema/tests/test_deprecations.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import referencing.exceptions
1111

12-
from jsonschema import FormatChecker, exceptions, validators
12+
from jsonschema import FormatChecker, exceptions, protocols, validators
1313

1414

1515
class TestDeprecations(TestCase):
@@ -64,6 +64,19 @@ def test_import_FormatError(self):
6464
self.assertEqual(FormatError, exceptions.FormatError)
6565
self.assertEqual(w.filename, __file__)
6666

67+
def test_import_Validator(self):
68+
"""
69+
As of v4.19.0, importing Validator from the package root is
70+
deprecated in favor of doing so from jsonschema.protocols.
71+
"""
72+
73+
message = "Importing Validator directly from the jsonschema package "
74+
with self.assertWarnsRegex(DeprecationWarning, message) as w:
75+
from jsonschema import Validator
76+
77+
self.assertEqual(Validator, protocols.Validator)
78+
self.assertEqual(w.filename, __file__)
79+
6780
def test_validators_validators(self):
6881
"""
6982
As of v4.0.0, accessing jsonschema.validators.validators is

0 commit comments

Comments
 (0)