From 303a77a7180b25f80b5411b7d020ef13164ce1aa Mon Sep 17 00:00:00 2001 From: Felix Schnabel Date: Tue, 14 Nov 2023 00:29:26 +0100 Subject: [PATCH 1/4] Fixup mypy 1.7.0 errors Signed-off-by: Felix Schnabel --- .gitignore | 1 + monai/apps/deepgrow/interaction.py | 2 +- monai/apps/pathology/metrics/lesion_froc.py | 5 ++++- monai/metrics/utils.py | 4 ++-- monai/transforms/io/array.py | 2 +- setup.cfg | 4 +++- 6 files changed, 12 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 8c66d4a651..437677d2bb 100644 --- a/.gitignore +++ b/.gitignore @@ -115,6 +115,7 @@ venv.bak/ examples/scd_lvsegs.npz temp/ .idea/ +.dmypy.json *~ diff --git a/monai/apps/deepgrow/interaction.py b/monai/apps/deepgrow/interaction.py index 88211c31e3..fa3a28bfef 100644 --- a/monai/apps/deepgrow/interaction.py +++ b/monai/apps/deepgrow/interaction.py @@ -49,7 +49,7 @@ def __init__( if not isinstance(transforms, Compose): transforms = Compose(transforms) - self.transforms = transforms + self.transforms: Compose = transforms self.max_interactions = max_interactions self.train = train self.key_probability = key_probability diff --git a/monai/apps/pathology/metrics/lesion_froc.py b/monai/apps/pathology/metrics/lesion_froc.py index 0899de9a76..89a404e384 100644 --- a/monai/apps/pathology/metrics/lesion_froc.py +++ b/monai/apps/pathology/metrics/lesion_froc.py @@ -11,7 +11,7 @@ from __future__ import annotations -from typing import TYPE_CHECKING +from typing import Any, Iterable, TYPE_CHECKING import numpy as np @@ -94,6 +94,9 @@ def prepare_inference_result(self, sample: dict) -> tuple[np.ndarray, np.ndarray nms_outputs = self.nms(probs_map=prob_map, resolution_level=sample["level"]) # separate nms outputs + probs: Iterable[Any] + x_coord: Iterable[Any] + y_coord: Iterable[Any] if nms_outputs: probs, x_coord, y_coord = zip(*nms_outputs) else: diff --git a/monai/metrics/utils.py b/monai/metrics/utils.py index c139fc35ed..4d4e6570c5 100644 --- a/monai/metrics/utils.py +++ b/monai/metrics/utils.py @@ -14,7 +14,7 @@ import warnings from functools import lru_cache, partial from types import ModuleType -from typing import Any, Sequence +from typing import Any, Iterable, Sequence import numpy as np import torch @@ -383,7 +383,7 @@ def remap_instance_id(pred: torch.Tensor, by_size: bool = False) -> torch.Tensor by_size: if True, largest instance will be assigned a smaller id. """ - pred_id = list(pred.unique()) + pred_id: Iterable[Any] = list(pred.unique()) # the original implementation has the limitation that if there is no 0 in pred, error will happen pred_id = [i for i in pred_id if i != 0] diff --git a/monai/transforms/io/array.py b/monai/transforms/io/array.py index b36c011822..cd7e4ef090 100644 --- a/monai/transforms/io/array.py +++ b/monai/transforms/io/array.py @@ -268,7 +268,7 @@ def __call__(self, filename: Sequence[PathLike] | PathLike, reader: ImageReader break if img is None or reader is None: - if isinstance(filename, tuple) and len(filename) == 1: + if isinstance(filename, Sequence) and len(filename) == 1: filename = filename[0] msg = "\n".join([f"{e}" for e in err]) raise RuntimeError( diff --git a/setup.cfg b/setup.cfg index d6c9b4f190..7699ed19dd 100644 --- a/setup.cfg +++ b/setup.cfg @@ -228,10 +228,12 @@ pretty = False # Warns about per-module sections in the config file that do not match any files processed when invoking mypy. warn_unused_configs = True # Make arguments prepended via Concatenate be truly positional-only. -strict_concatenate = True +extra_checks = True # Allows variables to be redefined with an arbitrary type, # as long as the redefinition is in the same block and nesting level as the original definition. # allow_redefinition = True +# ForkServerContext is not supported on Windows. +platform = linux exclude = venv/ From 338306d3e1c26670e1abf8a7d6b83739fc16e780 Mon Sep 17 00:00:00 2001 From: Felix Schnabel Date: Tue, 14 Nov 2023 00:48:22 +0100 Subject: [PATCH 2/4] Fix formatting Signed-off-by: Felix Schnabel --- monai/apps/pathology/metrics/lesion_froc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monai/apps/pathology/metrics/lesion_froc.py b/monai/apps/pathology/metrics/lesion_froc.py index 89a404e384..f4bf51ab28 100644 --- a/monai/apps/pathology/metrics/lesion_froc.py +++ b/monai/apps/pathology/metrics/lesion_froc.py @@ -11,7 +11,7 @@ from __future__ import annotations -from typing import Any, Iterable, TYPE_CHECKING +from typing import TYPE_CHECKING, Any, Iterable import numpy as np From 6aa5ead6d18d31e3ba82b370524f768342abd88f Mon Sep 17 00:00:00 2001 From: Felix Schnabel Date: Tue, 14 Nov 2023 08:24:31 +0100 Subject: [PATCH 3/4] Require mypy 1.5.0 or greater Signed-off-by: Felix Schnabel --- requirements-dev.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements-dev.txt b/requirements-dev.txt index 38715b8449..6332d5b0a5 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -22,7 +22,7 @@ isort>=5.1 ruff pytype>=2020.6.1; platform_system != "Windows" types-pkg_resources -mypy>=0.790 +mypy>=1.5.0 ninja torchvision psutil From 63909a562baf9e5d57734c4227930ff0994df737 Mon Sep 17 00:00:00 2001 From: Felix Schnabel Date: Tue, 14 Nov 2023 17:55:18 +0100 Subject: [PATCH 4/4] Remove fixed linux platform for mypy typechecking Signed-off-by: Felix Schnabel --- monai/apps/auto3dseg/data_analyzer.py | 2 +- setup.cfg | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/monai/apps/auto3dseg/data_analyzer.py b/monai/apps/auto3dseg/data_analyzer.py index 2c485f03eb..9280fb5be5 100644 --- a/monai/apps/auto3dseg/data_analyzer.py +++ b/monai/apps/auto3dseg/data_analyzer.py @@ -210,7 +210,7 @@ def get_all_case_stats(self, key="training", transform_list=None): nprocs = torch.cuda.device_count() logger.info(f"Found {nprocs} GPUs for data analyzing!") if nprocs > 1: - tmp_ctx = get_context("forkserver") + tmp_ctx: Any = get_context("forkserver") with tmp_ctx.Manager() as manager: manager_list = manager.list() processes = [] diff --git a/setup.cfg b/setup.cfg index 7699ed19dd..123da68dfa 100644 --- a/setup.cfg +++ b/setup.cfg @@ -232,8 +232,6 @@ extra_checks = True # Allows variables to be redefined with an arbitrary type, # as long as the redefinition is in the same block and nesting level as the original definition. # allow_redefinition = True -# ForkServerContext is not supported on Windows. -platform = linux exclude = venv/