Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use importlib.resources if possible #176

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, 9, 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
)