Skip to content

Commit

Permalink
D Add importlib-resources import for older python
Browse files Browse the repository at this point in the history
* `importlib.resources.files` and `importlib.resources.as_file` were
  added in py39
  • Loading branch information
ymilki committed Nov 18, 2023
1 parent 2c84fca commit 07b76f2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"jsonschema",
"pyyaml",
"typing-extensions",
"importlib-resources",
]


Expand Down
14 changes: 11 additions & 3 deletions swagger_spec_validator/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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


Expand Down
10 changes: 6 additions & 4 deletions tests/common_test.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)

0 comments on commit 07b76f2

Please sign in to comment.