Skip to content

Commit 14b4fd7

Browse files
authored
Merge pull request #873 from sevein/dev/issue-871-importlib-resources
Load schemas via importlib.resources
2 parents 272b4f2 + ffb5e06 commit 14b4fd7

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

jsonschema/_utils.py

Lines changed: 10 additions & 3 deletions
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):

setup.cfg

Lines changed: 1 addition & 0 deletions
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)