From 2c84fca5b1e3b73845e650b1c780485ba2d14ac3 Mon Sep 17 00:00:00 2001 From: milki Date: Fri, 17 Nov 2023 14:47:45 -0800 Subject: [PATCH 1/2] T Fix test expectation * Test is expecting the full path to the resource --- tests/common_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/common_test.py b/tests/common_test.py index d2724ac..74224e6 100644 --- a/tests/common_test.py +++ b/tests/common_test.py @@ -19,5 +19,5 @@ def test_read_resource_file(monkeypatch): read_resource_file(resource_path) m.assert_called_once_with( - importlib.resources.files("swagger_spec_validator") + importlib.resources.files("swagger_spec_validator") / resource_path ) From 07b76f2d022878cd8d212c90b060d52f4895a7ba Mon Sep 17 00:00:00 2001 From: milki Date: Fri, 17 Nov 2023 15:05:40 -0800 Subject: [PATCH 2/2] D Add `importlib-resources` import for older python * `importlib.resources.files` and `importlib.resources.as_file` were added in py39 --- setup.py | 1 + swagger_spec_validator/common.py | 14 +++++++++++--- tests/common_test.py | 10 ++++++---- 3 files changed, 18 insertions(+), 7 deletions(-) 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 74224e6..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") / resource_path - ) + m.assert_called_once_with(files("swagger_spec_validator") / resource_path)