Skip to content

Commit ffb5e06

Browse files
committed
Load schemas via importlib.resources
1 parent 272b4f2 commit ffb5e06

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

Diff for: jsonschema/_utils.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22
from urllib.parse import urlsplit
33
import itertools
44
import json
5-
import pkgutil
65
import re
6+
import sys
7+
8+
# The files() API was added in Python 3.9.
9+
if sys.version_info >= (3, 9): # pragma: no cover
10+
import importlib.resources as resources
11+
else: # pragma: no cover
12+
import importlib_resources as resources
713

814

915
class URIDict(MutableMapping):
@@ -51,8 +57,9 @@ def load_schema(name):
5157
Load a schema from ./schemas/``name``.json and return it.
5258
"""
5359

54-
data = pkgutil.get_data("jsonschema", "schemas/{0}.json".format(name))
55-
return json.loads(data.decode("utf-8"))
60+
path = resources.files(__package__).joinpath(f"schemas/{name}.json")
61+
data = path.read_text(encoding="utf-8")
62+
return json.loads(data)
5663

5764

5865
def format_as_index(container, indices):

Diff for: setup.cfg

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ python_requires = >=3.7
2828
install_requires =
2929
attrs>=17.4.0
3030
importlib_metadata;python_version<'3.8'
31+
importlib_resources;python_version<'3.9'
3132
pyrsistent>=0.14.0,!=0.17.0,!=0.17.1,!=0.17.2
3233

3334
[options.extras_require]

0 commit comments

Comments
 (0)