Skip to content

Commit 07e38b5

Browse files
authored
Use mypy for type checking (#249)
* add typechecking with mypy * typecheck packages without __init__ * reformat code * add mypy to lock file * resolve circular dependencies and fix bugs * refactor * add typecheck to CI * change initial value to None * test if CI passes without typecheck * add type check to CI * git status to CI for more output * ignore mypy cache * reformat code * add assertion for non optional parameter * remove redundant imports
1 parent e1f73cc commit 07e38b5

31 files changed

+1140
-611
lines changed

.github/workflows/main.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ jobs:
3434

3535
- name: Lint code
3636
run: poetry run pylint -j4 wkcuber
37+
38+
- name: Check typing
39+
run: |
40+
./typecheck.sh
3741
3842
- name: Python tests
3943
run: poetry run pytest tests

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,7 @@ testdata/WT1_wkw
9999
testdata/tiff_mag_2_reference
100100

101101
# VSCode
102-
.vscode/
102+
.vscode/
103+
104+
# MyPy
105+
.mypy_cache/

poetry.lock

Lines changed: 48 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ natsort = "^6.2.0"
1818
psutil = "^5.6.7"
1919
nibabel = "^2.5.1"
2020
scikit-image = "^0.16.2"
21+
mypy = "^0.770"
2122

2223
[tool.poetry.dev-dependencies]
2324
pylint = "^2.6.0"

setup.cfg

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[mypy]
2+
ignore_missing_imports = True
3+
4+
[mypy-wkcuber.vendor.*]
5+
ignore_errors = True

tests/test_downsampling.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import logging
2+
from typing import Tuple
3+
24
import numpy as np
35
from wkcuber.downsampling import (
46
InterpolationModes,
@@ -20,7 +22,9 @@
2022
target_info = WkwDatasetInfo("testoutput/WT1_wkw", "color", 2, wkw.Header(np.uint8))
2123

2224

23-
def read_wkw(wkw_info, offset, size):
25+
def read_wkw(
26+
wkw_info: WkwDatasetInfo, offset: Tuple[int, int, int], size: Tuple[int, int, int]
27+
):
2428
with open_wkw(wkw_info) as wkw_dataset:
2529
return wkw_dataset.read(offset, size)
2630

@@ -120,7 +124,7 @@ def downsample_test_helper(use_compress):
120124

121125
assert np.all(
122126
target_buffer
123-
== downsample_cube(source_buffer, (2, 2, 2), InterpolationModes.MAX)
127+
== downsample_cube(source_buffer, [2, 2, 2], InterpolationModes.MAX)
124128
)
125129

126130

@@ -180,7 +184,7 @@ def test_downsample_multi_channel():
180184
for channel_index in range(num_channels):
181185
channels.append(
182186
downsample_cube(
183-
source_data[channel_index], (2, 2, 2), InterpolationModes.MAX
187+
source_data[channel_index], [2, 2, 2], InterpolationModes.MAX
184188
)
185189
)
186190
joined_buffer = np.stack(channels)

typecheck.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
set -eEuo pipefail
3+
python -m mypy wkcuber --disallow-untyped-defs --show-error-codes --strict-equality --namespace-packages

wkcuber/__main__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
from .metadata import write_webknossos_metadata, refresh_metadata
55
from .utils import add_isotropic_flag, setup_logging, add_scale_flag
66
from .mag import Mag
7+
from argparse import Namespace, ArgumentParser
78

89

9-
def create_parser():
10+
def create_parser() -> ArgumentParser:
1011
parser = create_cubing_parser()
1112

1213
parser.add_argument(
@@ -32,7 +33,7 @@ def create_parser():
3233
return parser
3334

3435

35-
def main(args):
36+
def main(args: Namespace) -> None:
3637
setup_logging(args)
3738

3839
bounding_box = cubing(
@@ -82,5 +83,5 @@ def main(args):
8283

8384

8485
if __name__ == "__main__":
85-
parsed_args = create_parser().parse_args()
86+
parsed_args: Namespace = create_parser().parse_args()
8687
main(parsed_args)

0 commit comments

Comments
 (0)