Project-MONAI / MONAI

AI Toolkit for Healthcare Imaging
https://monai.io/
Apache License 2.0
5.77k stars 1.06k forks source link

MyPy issue #8149

Open KumoLiu opened 4 days ago

KumoLiu commented 4 days ago
mypy 1.12.0 (compiled: yes)
monai/data/utils.py:562:17: error: Item "ItemsView[Any, Any]" of "Union[Iterator[tuple[Any, Any]], ItemsView[Any, Any], enumerate[Any]]" has no attribute "__next__"  [union-attr]
monai/transforms/utils.py:1130:25: error: Incompatible types in assignment (expression has type "Union[int, float, Any]", target has type "int")  [assignment]
monai/transforms/utils.py:1131:23: error: Incompatible types in assignment (expression has type "Union[int, float, Any]", target has type "int")  [assignment]
monai/transforms/croppad/functional.py:194:17: error: Incompatible types in assignment (expression has type "Union[Any, Size]", variable has type "list[Any]")  [assignment]
monai/transforms/utility/dictionary.py:738:23: error: Item "Hashable" of "Union[Any, Collection[Hashable], Hashable]" has no attribute "__iter__" (not iterable)  [union-attr]
monai/transforms/io/array.py:503:30: error: "str" not callable  [operator]
monai/transforms/io/array.py:507:35: error: Incompatible types in assignment (expression has type "Union[Any, ImageWriter]", variable has type "None")  [assignment]
monai/metrics/utils.py:206:20: error: Incompatible return value type (got "Union[tuple[Any, Any], tuple[Any, Any, Any, Any]]", expected "tuple[NdarrayTensor, NdarrayTensor]")  [return-value]
monai/bundle/reference_resolver.py:245:21: error: Item "dict_items[Any, Any]" of "Union[Iterator[tuple[Any, Any]], dict_items[Any, Any], enumerate[Any]]" has no attribute "__next__"  [union-attr]
monai/bundle/reference_resolver.py:247:13: error: Incompatible types in "yield" (actual type "tuple[Union[Any, int], str, Any]", expected type "tuple[str, str, Any]")  [misc]
monai/apps/deepgrow/dataset.py:159:45: error: Argument 1 to "sum" has incompatible type "Optional[Any]"; expected "Union[_SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]]]"  [arg-type]
monai/apps/deepgrow/dataset.py:178:35: error: Item "None" of "Optional[Any]" has no attribute "flatten"  [union-attr]
tests/test_resample_backends.py:36:80: error: List item 4 has incompatible type "tuple[int, int, int, int]"; expected "Union[tuple[int, int, int], str, dict[str, Optional[str]], type[floating[Any]], type[floating[Any]]]"  [list-item]
tests/test_resample_backends.py:41:60: error: List item 1 has incompatible type "Union[type[int], type[float], type[floating[Any]], type[floating[Any]]]"; expected "Union[tuple[int, int, int], str, dict[str, Optional[str]], type[floating[Any]], type[floating[Any]]]"  [list-item]
tests/test_resample_backends.py:42:60: error: List item 1 has incompatible type "Union[type[int], type[float], type[floating[Any]], type[floating[Any]]]"; expected "Union[tuple[int, int, int], str, dict[str, Optional[str]], type[floating[Any]], type[floating[Any]]]"  [list-item]
tests/test_resample_backends.py:42:82: error: List item 4 has incompatible type "tuple[int, int, int, int]"; expected "Union[tuple[int, int, int], str, dict[str, Optional[str]], type[floating[Any]], type[floating[Any]]]"  [list-item]
monai/apps/pathology/transforms/post/dictionary.py:406:47: error: Argument 2 to "__call__" of "GenerateInstanceCentroid" has incompatible type "Optional[Any]"; expected "Union[Sequence[int], int]"  [arg-type]
monai/apps/generation/maisi/networks/autoencoderkl_maisi.py:241:12: error: Unsupported operand types for % ("int" and "Sequence[int]")  [operator]
monai/apps/generation/maisi/networks/autoencoderkl_maisi.py:241:12: note: Right operand is of type "Union[Any, Sequence[int], int]"
monai/apps/generation/maisi/networks/autoencoderkl_maisi.py:242:23: error: Unsupported operand types for * ("int" and "Sequence[int]")  [operator]
monai/apps/generation/maisi/networks/autoencoderkl_maisi.py:242:23: note: Both left and right operands are unions
monai/apps/generation/maisi/networks/autoencoderkl_maisi.py:242:24: error: Unsupported operand types for // ("int" and "Sequence[int]")  [operator]
monai/apps/generation/maisi/networks/autoencoderkl_maisi.py:242:24: note: Right operand is of type "Union[Any, Sequence[int], int]"
tests/test_trt_compile.py:84:13: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
Found 20 errors in 11 files (checked 1199 source files)
KumoLiu commented 4 days ago

related to the new release from mypy: https://pypi.org/project/mypy/#history

KumoLiu commented 1 day ago
mypy 1.11.2 (compiled: yes)
monai/networks/utils.py:700:15: error: Argument "f" to "export" has incompatible type "BytesIO"; expected "Union[str, PathLike[Any], None]"  [arg-type]
KumoLiu commented 1 day ago
mypy 1.11.2 (compiled: yes)
monai/networks/utils.py:700:15: error: Argument "f" to "export" has incompatible type "BytesIO"; expected "Union[str, PathLike[Any], None]"  [arg-type]

Hi @borisfom and @binliunls, It seems that since version 2.5.0, the f parameter in torch.onnx.export no longer accepts BytesIO. Do you have any suggestions on how we can address this mypy issue? https://github.com/pytorch/pytorch/blob/v2.5.0/torch/onnx/utils.py#L178 https://github.com/Project-MONAI/MONAI/blob/dev/monai/networks/utils.py#L693

Ignore it as a workaround in this pr: https://github.com/Project-MONAI/MONAI/pull/8161 to ensure the ci pass. We can fix it later.