diff --git a/setup.py b/setup.py index f6e7683..45f645b 100644 --- a/setup.py +++ b/setup.py @@ -13,6 +13,7 @@ "jsonschema", "pyyaml", "typing-extensions", + "importlib-resources", ] diff --git a/swagger_spec_validator/common.py b/swagger_spec_validator/common.py index 5989c06..0b2ad88 100644 --- a/swagger_spec_validator/common.py +++ b/swagger_spec_validator/common.py @@ -13,7 +13,15 @@ from urllib.request import urlopen import yaml -import importlib.resources + +try: + from importlib.resources import files # type: ignore + from importlib.resources import as_file # type: ignore +except ImportError: # pragma: cover + from importlib_resources import files + from importlib_resources import as_file + + from typing_extensions import ParamSpec try: @@ -55,8 +63,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 = files("swagger_spec_validator") / resource_path + with as_file(ref) as path: return read_file(path), path diff --git a/tests/common_test.py b/tests/common_test.py index d2724ac..3d3989b 100644 --- a/tests/common_test.py +++ b/tests/common_test.py @@ -1,7 +1,11 @@ +import importlib.resources import uuid from unittest import mock -import importlib.resources +try: + from importlib.resources import files # ignore: type +except ImportError: # pragma: cover + from importlib_resources import files # ignore: type from swagger_spec_validator.common import read_file from swagger_spec_validator.common import read_resource_file @@ -18,6 +22,4 @@ def test_read_resource_file(monkeypatch): read_resource_file(resource_path) read_resource_file(resource_path) - m.assert_called_once_with( - importlib.resources.files("swagger_spec_validator") - ) + m.assert_called_once_with(files("swagger_spec_validator") / resource_path)