Skip to content
Merged
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
37 changes: 37 additions & 0 deletions .github/workflows/mypy-type-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# This workflow will perform code type checking using mypy

name: mypy type checking

on:
push:
branches: [ develop, pre-release, master, main ]
pull_request:
branches: [ develop, pre-release, master, main ]

jobs:

build:

runs-on: ubuntu-22.04

strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11"]

steps:

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}

- name: Checkout repository
uses: actions/checkout@v3

- name: Setup mypy
run: |
pip install mypy

- name: Perform type checking
run: |
mypy --install-types --non-interactive --follow-imports=skip tiatoolbox/__init__.py
11 changes: 11 additions & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,14 @@ def test_lazy_import() -> None:
)

assert "exceptions" in sys.modules


def test_lazy_import_module_not_found() -> None:
"""'Test lazy import for ModuleNotFoundError."""
from tiatoolbox import _lazy_import

with pytest.raises(ModuleNotFoundError):
_lazy_import(
"nonexistent_module",
Path(__file__).parent.parent / "tiatoolbox",
)
10 changes: 8 additions & 2 deletions tiatoolbox/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
if sys.version_info >= (3, 9): # pragma: no cover
import importlib.resources as importlib_resources
else: # pragma: no cover
import importlib_resources # To support Python 3.8
# To support Python 3.8
import importlib_resources # type: ignore[import-not-found]

import yaml

if TYPE_CHECKING: # pragma: no cover
from logging import LogRecord
from types import ModuleType

__author__ = """TIA Centre"""
__email__ = "[email protected]"
Expand Down Expand Up @@ -71,6 +73,7 @@ def filter(self: DuplicateFilter, record: LogRecord) -> bool:


# runtime context parameters
rcParam: dict[str, str | Path | dict] # noqa: N816
rcParam = { # noqa: N816
"TIATOOLBOX_HOME": Path.home() / ".tiatoolbox",
}
Expand All @@ -88,6 +91,7 @@ def read_registry_files(path_to_registry: str | Path) -> dict | str:


"""
path_to_registry = str(path_to_registry) # To pass tests with Python 3.8
pretrained_files_registry_path = importlib_resources.as_file(
importlib_resources.files("tiatoolbox") / path_to_registry,
)
Expand All @@ -101,8 +105,10 @@ def read_registry_files(path_to_registry: str | Path) -> dict | str:
rcParam["pretrained_model_info"] = read_registry_files("data/pretrained_model.yaml")


def _lazy_import(name: str, module_location: Path) -> sys.modules:
def _lazy_import(name: str, module_location: Path) -> ModuleType:
spec = importlib.util.spec_from_file_location(name, module_location)
if spec is None or spec.loader is None:
raise ModuleNotFoundError(name=name, path=str(module_location))
loader = importlib.util.LazyLoader(spec.loader)
spec.loader = loader
module = importlib.util.module_from_spec(spec)
Expand Down