Skip to content

Commit

Permalink
Deprecate importing Validator from the package root.
Browse files Browse the repository at this point in the history
Import it from jsonschema.protocols instead.
  • Loading branch information
Julian committed Aug 6, 2023
1 parent 8a55721 commit eaf2e7b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
v4.19.0
=======

* Importing the ``Validator`` protocol directly from the package root is deprecated.
Import it from ``jsonschema.protocols.Validator`` instead.

v4.18.6
=======

Expand Down
12 changes: 10 additions & 2 deletions jsonschema/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from jsonschema._format import FormatChecker
from jsonschema._types import TypeChecker
from jsonschema.exceptions import SchemaError, ValidationError
from jsonschema.protocols import Validator
from jsonschema.validators import (
Draft3Validator,
Draft4Validator,
Expand Down Expand Up @@ -65,6 +64,16 @@ def __getattr__(name):
)
from jsonschema.exceptions import FormatError
return FormatError
elif name == "Validator":
warnings.warn(
"Importing Validator directly from the jsonschema package "
"is deprecated and will become an ImportError. Import it from "
"jsonschema.protocols instead.",
DeprecationWarning,
stacklevel=2,
)
from jsonschema.protocols import Validator
return Validator
elif name == "RefResolutionError":
from jsonschema.exceptions import _RefResolutionError
warnings.warn(
Expand Down Expand Up @@ -107,6 +116,5 @@ def __getattr__(name):
"SchemaError",
"TypeChecker",
"ValidationError",
"Validator",
"validate",
]
15 changes: 14 additions & 1 deletion jsonschema/tests/test_deprecations.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import referencing.exceptions

from jsonschema import FormatChecker, exceptions, validators
from jsonschema import FormatChecker, exceptions, protocols, validators


class TestDeprecations(TestCase):
Expand Down Expand Up @@ -64,6 +64,19 @@ def test_import_FormatError(self):
self.assertEqual(FormatError, exceptions.FormatError)
self.assertEqual(w.filename, __file__)

def test_import_Validator(self):
"""
As of v4.19.0, importing Validator from the package root is
deprecated in favor of doing so from jsonschema.protocols.
"""

message = "Importing Validator directly from the jsonschema package "
with self.assertWarnsRegex(DeprecationWarning, message) as w:
from jsonschema import Validator

self.assertEqual(Validator, protocols.Validator)
self.assertEqual(w.filename, __file__)

def test_validators_validators(self):
"""
As of v4.0.0, accessing jsonschema.validators.validators is
Expand Down

0 comments on commit eaf2e7b

Please sign in to comment.