Skip to content

Commit e242c44

Browse files
committed
Fix type hint for image reader
1 parent e267705 commit e242c44

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

monai/data/image_reader.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from collections.abc import Callable, Iterable, Iterator, Sequence
2323
from dataclasses import dataclass
2424
from pathlib import Path
25-
from typing import TYPE_CHECKING, Any
25+
from typing import TYPE_CHECKING, Any, Union
2626

2727
import numpy as np
2828
from torch.utils.data._utils.collate import np_str_obj_array_pattern
@@ -57,6 +57,13 @@
5757
cp, has_cp = optional_import("cupy")
5858
kvikio, has_kvikio = optional_import("kvikio")
5959

60+
if TYPE_CHECKING:
61+
import cupy
62+
63+
NdarrayOrCupy = Union[np.ndarray, cupy.ndarray]
64+
else:
65+
NdarrayOrCupy = Any
66+
6067
__all__ = ["ImageReader", "ITKReader", "NibabelReader", "NumpyReader", "PILReader", "PydicomReader", "NrrdReader"]
6168

6269

@@ -663,10 +670,7 @@ def get_data(self, data) -> tuple[np.ndarray, dict]:
663670
metadata[MetaKeys.SPATIAL_SHAPE] = data_array.shape
664671
dicom_data.append((data_array, metadata))
665672

666-
# TODO: the actual type is list[np.ndarray | cp.ndarray]
667-
# should figure out how to define correct types without having cupy not found error
668-
# https://github.com/Project-MONAI/MONAI/pull/8188#discussion_r1886645918
669-
img_array: list[np.ndarray] = []
673+
img_array: list[NdarrayOrCupy] = []
670674
compatible_meta: dict = {}
671675

672676
for data_array, metadata in ensure_tuple(dicom_data):
@@ -1104,10 +1108,7 @@ def get_data(self, img) -> tuple[np.ndarray, dict]:
11041108
img: a Nibabel image object loaded from an image file or a list of Nibabel image objects.
11051109
11061110
"""
1107-
# TODO: the actual type is list[np.ndarray | cp.ndarray]
1108-
# should figure out how to define correct types without having cupy not found error
1109-
# https://github.com/Project-MONAI/MONAI/pull/8188#discussion_r1886645918
1110-
img_array: list[np.ndarray] = []
1111+
img_array: list[NdarrayOrCupy] = []
11111112
compatible_meta: dict = {}
11121113

11131114
for i, filename in zip(ensure_tuple(img), self.filenames):

0 commit comments

Comments
 (0)