Skip to content

Commit

Permalink
Use mypy for type checking (#249)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
rschwanhold authored Jan 8, 2021
1 parent e1f73cc commit 07e38b5
Show file tree
Hide file tree
Showing 31 changed files with 1,140 additions and 611 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ jobs:

- name: Lint code
run: poetry run pylint -j4 wkcuber

- name: Check typing
run: |
./typecheck.sh
- name: Python tests
run: poetry run pytest tests
Expand Down
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,7 @@ testdata/WT1_wkw
testdata/tiff_mag_2_reference

# VSCode
.vscode/
.vscode/

# MyPy
.mypy_cache/
55 changes: 48 additions & 7 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ natsort = "^6.2.0"
psutil = "^5.6.7"
nibabel = "^2.5.1"
scikit-image = "^0.16.2"
mypy = "^0.770"

[tool.poetry.dev-dependencies]
pylint = "^2.6.0"
Expand Down
5 changes: 5 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[mypy]
ignore_missing_imports = True

[mypy-wkcuber.vendor.*]
ignore_errors = True
10 changes: 7 additions & 3 deletions tests/test_downsampling.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import logging
from typing import Tuple

import numpy as np
from wkcuber.downsampling import (
InterpolationModes,
Expand All @@ -20,7 +22,9 @@
target_info = WkwDatasetInfo("testoutput/WT1_wkw", "color", 2, wkw.Header(np.uint8))


def read_wkw(wkw_info, offset, size):
def read_wkw(
wkw_info: WkwDatasetInfo, offset: Tuple[int, int, int], size: Tuple[int, int, int]
):
with open_wkw(wkw_info) as wkw_dataset:
return wkw_dataset.read(offset, size)

Expand Down Expand Up @@ -120,7 +124,7 @@ def downsample_test_helper(use_compress):

assert np.all(
target_buffer
== downsample_cube(source_buffer, (2, 2, 2), InterpolationModes.MAX)
== downsample_cube(source_buffer, [2, 2, 2], InterpolationModes.MAX)
)


Expand Down Expand Up @@ -180,7 +184,7 @@ def test_downsample_multi_channel():
for channel_index in range(num_channels):
channels.append(
downsample_cube(
source_data[channel_index], (2, 2, 2), InterpolationModes.MAX
source_data[channel_index], [2, 2, 2], InterpolationModes.MAX
)
)
joined_buffer = np.stack(channels)
Expand Down
3 changes: 3 additions & 0 deletions typecheck.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
set -eEuo pipefail
python -m mypy wkcuber --disallow-untyped-defs --show-error-codes --strict-equality --namespace-packages
7 changes: 4 additions & 3 deletions wkcuber/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
from .metadata import write_webknossos_metadata, refresh_metadata
from .utils import add_isotropic_flag, setup_logging, add_scale_flag
from .mag import Mag
from argparse import Namespace, ArgumentParser


def create_parser():
def create_parser() -> ArgumentParser:
parser = create_cubing_parser()

parser.add_argument(
Expand All @@ -32,7 +33,7 @@ def create_parser():
return parser


def main(args):
def main(args: Namespace) -> None:
setup_logging(args)

bounding_box = cubing(
Expand Down Expand Up @@ -82,5 +83,5 @@ def main(args):


if __name__ == "__main__":
parsed_args = create_parser().parse_args()
parsed_args: Namespace = create_parser().parse_args()
main(parsed_args)
Loading

0 comments on commit 07e38b5

Please sign in to comment.