Skip to content

Commit 67aeb56

Browse files
Jiaqi-Lvshaneahmedpre-commit-ci[bot]
authored
🚧 Add Type Checking using mypy (#723)
- Add type checking using `mypy`, a static type checker for Python. - A GitHub action has been created to run `mypy` after push and pull requests. - Currently, only `tiatoolbox/__init__.py` has been checked to work without errors with `mypy`. --------- Co-authored-by: Shan E Ahmed Raza <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 2e9802b commit 67aeb56

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# This workflow will perform code type checking using mypy
2+
3+
name: mypy type checking
4+
5+
on:
6+
push:
7+
branches: [ develop, pre-release, master, main ]
8+
pull_request:
9+
branches: [ develop, pre-release, master, main ]
10+
11+
jobs:
12+
13+
build:
14+
15+
runs-on: ubuntu-22.04
16+
17+
strategy:
18+
matrix:
19+
python-version: ["3.8", "3.9", "3.10", "3.11"]
20+
21+
steps:
22+
23+
- name: Set up Python ${{ matrix.python-version }}
24+
uses: actions/setup-python@v3
25+
with:
26+
python-version: ${{ matrix.python-version }}
27+
28+
- name: Checkout repository
29+
uses: actions/checkout@v3
30+
31+
- name: Setup mypy
32+
run: |
33+
pip install mypy
34+
35+
- name: Perform type checking
36+
run: |
37+
mypy --install-types --non-interactive --follow-imports=skip tiatoolbox/__init__.py

tests/test_init.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,14 @@ def test_lazy_import() -> None:
139139
)
140140

141141
assert "exceptions" in sys.modules
142+
143+
144+
def test_lazy_import_module_not_found() -> None:
145+
"""'Test lazy import for ModuleNotFoundError."""
146+
from tiatoolbox import _lazy_import
147+
148+
with pytest.raises(ModuleNotFoundError):
149+
_lazy_import(
150+
"nonexistent_module",
151+
Path(__file__).parent.parent / "tiatoolbox",
152+
)

tiatoolbox/__init__.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
if sys.version_info >= (3, 9): # pragma: no cover
1010
import importlib.resources as importlib_resources
1111
else: # pragma: no cover
12-
import importlib_resources # To support Python 3.8
12+
# To support Python 3.8
13+
import importlib_resources # type: ignore[import-not-found]
1314

1415
import yaml
1516

1617
if TYPE_CHECKING: # pragma: no cover
1718
from logging import LogRecord
19+
from types import ModuleType
1820

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

7274

7375
# runtime context parameters
76+
rcParam: dict[str, str | Path | dict] # noqa: N816
7477
rcParam = { # noqa: N816
7578
"TIATOOLBOX_HOME": Path.home() / ".tiatoolbox",
7679
}
@@ -88,6 +91,7 @@ def read_registry_files(path_to_registry: str | Path) -> dict | str:
8891
8992
9093
"""
94+
path_to_registry = str(path_to_registry) # To pass tests with Python 3.8
9195
pretrained_files_registry_path = importlib_resources.as_file(
9296
importlib_resources.files("tiatoolbox") / path_to_registry,
9397
)
@@ -101,8 +105,10 @@ def read_registry_files(path_to_registry: str | Path) -> dict | str:
101105
rcParam["pretrained_model_info"] = read_registry_files("data/pretrained_model.yaml")
102106

103107

104-
def _lazy_import(name: str, module_location: Path) -> sys.modules:
108+
def _lazy_import(name: str, module_location: Path) -> ModuleType:
105109
spec = importlib.util.spec_from_file_location(name, module_location)
110+
if spec is None or spec.loader is None:
111+
raise ModuleNotFoundError(name=name, path=str(module_location))
106112
loader = importlib.util.LazyLoader(spec.loader)
107113
spec.loader = loader
108114
module = importlib.util.module_from_spec(spec)

0 commit comments

Comments
 (0)