From eaf2e7b4559f100955108b500eb9d6444c1735d3 Mon Sep 17 00:00:00 2001 From: Julian Berman Date: Sun, 6 Aug 2023 18:03:50 +0100 Subject: [PATCH] Deprecate importing Validator from the package root. Import it from jsonschema.protocols instead. --- CHANGELOG.rst | 6 ++++++ jsonschema/__init__.py | 12 ++++++++++-- jsonschema/tests/test_deprecations.py | 15 ++++++++++++++- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 71dffc6a6..63e833063 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -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 ======= diff --git a/jsonschema/__init__.py b/jsonschema/__init__.py index db5a2cdf3..79924cf7e 100644 --- a/jsonschema/__init__.py +++ b/jsonschema/__init__.py @@ -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, @@ -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( @@ -107,6 +116,5 @@ def __getattr__(name): "SchemaError", "TypeChecker", "ValidationError", - "Validator", "validate", ] diff --git a/jsonschema/tests/test_deprecations.py b/jsonschema/tests/test_deprecations.py index aa108c05e..237d80dcc 100644 --- a/jsonschema/tests/test_deprecations.py +++ b/jsonschema/tests/test_deprecations.py @@ -9,7 +9,7 @@ import referencing.exceptions -from jsonschema import FormatChecker, exceptions, validators +from jsonschema import FormatChecker, exceptions, protocols, validators class TestDeprecations(TestCase): @@ -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