Skip to content

Commit 656a86a

Browse files
committed
Deprecate jsonschema.__version__.
importlib.metadata is in the standard library now, so there's not much purpose in pre-calculating this attribute.
1 parent b888520 commit 656a86a

File tree

6 files changed

+55
-15
lines changed

6 files changed

+55
-15
lines changed

docs/conf.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1+
from importlib import metadata
12
import os
23
import re
34
import sys
45

5-
import jsonschema
6-
76
# If extensions (or modules to document with autodoc) are in another directory,
87
# add these directories to sys.path here. If the directory is relative to the
98
# documentation root, use os.path.abspath to make it absolute, like shown here.
@@ -55,7 +54,7 @@
5554
#
5655
# version: The short X.Y version
5756
# release: The full version, including alpha/beta/rc tags.
58-
release = jsonschema.__version__
57+
release = metadata.version("jsonschema")
5958
version = release.partition("-")[0]
6059

6160
# There are two options for replacing |today|: either, you set today to some

docs/jsonschema_role.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from datetime import datetime
2+
from importlib import metadata
23
from urllib.parse import urljoin
34
import errno
45
import os
@@ -8,8 +9,6 @@
89
from lxml import html
910
import certifi
1011

11-
import jsonschema
12-
1312
__version__ = "1.2.0"
1413

1514
BASE_URL = "https://json-schema.org/draft-07/"
@@ -57,7 +56,8 @@ def fetch_or_load(spec_path):
5756

5857
headers = {
5958
"User-Agent": "python-jsonschema v{} - documentation build v{}".format(
60-
jsonschema.__version__, __version__,
59+
metadata.version("jsonschema"),
60+
__version__,
6161
),
6262
}
6363

jsonschema/__init__.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
Most commonly, `validate` is the quickest way to simply validate a given
88
instance under a schema, and will create a validator for you.
99
"""
10+
import warnings
1011

1112
from jsonschema._format import (
1213
FormatChecker,
@@ -36,8 +37,20 @@
3637
validate,
3738
)
3839

39-
try:
40-
from importlib import metadata
41-
except ImportError:
42-
import importlib_metadata as metadata
43-
__version__ = metadata.version("jsonschema")
40+
41+
def __getattr__(name):
42+
if name == "__version__":
43+
warnings.warn(
44+
"Accessing jsonschema.__version__ is deprecated and will be "
45+
"removed in a future release. Use importlib.metadata directly "
46+
"to query for jsonschema's version.",
47+
DeprecationWarning,
48+
)
49+
50+
try:
51+
from importlib import metadata
52+
except ImportError:
53+
import importlib_metadata as metadata
54+
55+
return metadata.version("jsonschema")
56+
raise AttributeError(f"module {__name__} has no attribute {name}")

jsonschema/cli.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@
1010
import sys
1111
import traceback
1212

13+
try:
14+
from importlib import metadata
15+
except ImportError:
16+
import importlib_metadata as metadata
17+
1318
import attr
1419

15-
from jsonschema import __version__
1620
from jsonschema._reflect import namedAny
1721
from jsonschema.exceptions import SchemaError
1822
from jsonschema.validators import RefResolver, validator_for
@@ -189,7 +193,7 @@ def _namedAnyWithDefault(name):
189193
parser.add_argument(
190194
"--version",
191195
action="version",
192-
version=__version__,
196+
version=metadata.version("jsonschema"),
193197
)
194198
parser.add_argument(
195199
"schema",

jsonschema/tests/test_cli.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@
1010
import sys
1111
import tempfile
1212

13+
try:
14+
from importlib import metadata
15+
except ImportError:
16+
import importlib_metadata as metadata
17+
1318
from pyrsistent import m
1419

15-
from jsonschema import Draft4Validator, Draft202012Validator, __version__, cli
20+
from jsonschema import Draft4Validator, Draft202012Validator, cli
1621
from jsonschema.exceptions import (
1722
RefResolutionError,
1823
SchemaError,
@@ -896,7 +901,7 @@ def test_version(self):
896901
stderr=subprocess.STDOUT,
897902
)
898903
version = version.decode("utf-8").strip()
899-
self.assertEqual(version, __version__)
904+
self.assertEqual(version, metadata.version("jsonschema"))
900905

901906
def test_no_arguments_shows_usage_notes(self):
902907
output = subprocess.check_output(

jsonschema/tests/test_deprecations.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from unittest import TestCase
2+
3+
import jsonschema
4+
5+
6+
class TestDeprecations(TestCase):
7+
def test_jsonschema_version(self):
8+
"""
9+
As of v4.0.0, __version__ is deprecated in favor of importlib.metadata.
10+
"""
11+
12+
with self.assertWarns(DeprecationWarning) as w:
13+
jsonschema.__version__
14+
15+
self.assertTrue(
16+
str(w.warning).startswith(
17+
"Accessing jsonschema.__version__ is deprecated",
18+
),
19+
)

0 commit comments

Comments
 (0)