Skip to content

Commit

Permalink
Use importlib.resources if available
Browse files Browse the repository at this point in the history
The importlib_resources package isn’t required from Python 3.7 on, it’s
available in importlib.resources.

Signed-off-by: Nils Philippsen <[email protected]>
  • Loading branch information
nphilipp committed Aug 22, 2024
1 parent abf4cde commit f76d5f3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import sys

from setuptools import find_packages, setup

Expand All @@ -13,9 +14,11 @@
"jsonschema",
"pyyaml",
"typing-extensions",
"importlib-resources >= 1.3",
]

if sys.version_info < (3, 7, 0):
install_requires.append("importlib-resources >= 1.3")


setup(
name=about["__title__"],
Expand Down
12 changes: 9 additions & 3 deletions swagger_spec_validator/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@
from urllib.request import pathname2url
from urllib.request import urlopen

import importlib_resources
try:
import importlib.resources
except ImportError:
import importlib
import importlib_resources
importlib.resources = importlib_resources

import yaml
from typing_extensions import ParamSpec

Expand Down Expand Up @@ -55,8 +61,8 @@ def read_file(file_path: str) -> dict[str, Any]:

@lru_cache
def read_resource_file(resource_path: str) -> tuple[dict[str, Any], str]:
ref = importlib_resources.files("swagger_spec_validator") / resource_path
with importlib_resources.as_file(ref) as path:
ref = importlib.resources.files("swagger_spec_validator") / resource_path
with importlib.resources.as_file(ref) as path:
return read_file(path), path


Expand Down
9 changes: 7 additions & 2 deletions tests/common_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import uuid
from unittest import mock

import importlib_resources
try:
import importlib.resources
except ImportError:
import importlib
import importlib_resources
importlib.resources = importlib_resources

from swagger_spec_validator.common import read_file
from swagger_spec_validator.common import read_resource_file
Expand All @@ -19,5 +24,5 @@ def test_read_resource_file(monkeypatch):
read_resource_file(resource_path)

m.assert_called_once_with(
importlib_resources.files("swagger_spec_validator") / resource_path
importlib.resources.files("swagger_spec_validator") / resource_path
)

0 comments on commit f76d5f3

Please sign in to comment.