From f76d5f3c4a338396e55ad799af7619c530cd56d3 Mon Sep 17 00:00:00 2001 From: Nils Philippsen Date: Thu, 22 Aug 2024 14:17:45 +0200 Subject: [PATCH] Use importlib.resources if available MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The importlib_resources package isn’t required from Python 3.7 on, it’s available in importlib.resources. Signed-off-by: Nils Philippsen --- setup.py | 5 ++++- swagger_spec_validator/common.py | 12 +++++++++--- tests/common_test.py | 9 +++++++-- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/setup.py b/setup.py index eab451e..a1346b8 100644 --- a/setup.py +++ b/setup.py @@ -1,4 +1,5 @@ import os +import sys from setuptools import find_packages, setup @@ -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__"], diff --git a/swagger_spec_validator/common.py b/swagger_spec_validator/common.py index 654cc57..d021d3c 100644 --- a/swagger_spec_validator/common.py +++ b/swagger_spec_validator/common.py @@ -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 @@ -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 diff --git a/tests/common_test.py b/tests/common_test.py index 0db4548..30d5884 100644 --- a/tests/common_test.py +++ b/tests/common_test.py @@ -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 @@ -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 )