Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix type hint for image reader
Signed-off-by: Tristan Kirscher <85068746+Kirscher@users.noreply.github.com>
  • Loading branch information
Kirscher committed Nov 30, 2025
commit bf06a4f0081b77588dda2624a751da46c4ea3b6d
19 changes: 10 additions & 9 deletions 19 monai/data/image_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from collections.abc import Callable, Iterable, Iterator, Sequence
from dataclasses import dataclass
from pathlib import Path
from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING, Any, Union

import numpy as np
from torch.utils.data._utils.collate import np_str_obj_array_pattern
Expand Down Expand Up @@ -57,6 +57,13 @@
cp, has_cp = optional_import("cupy")
kvikio, has_kvikio = optional_import("kvikio")

if TYPE_CHECKING:
import cupy

NdarrayOrCupy = Union[np.ndarray, cupy.ndarray]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was done to get around Cupy being an optional dependency, this makes it necessary when type checking but this might not be avoidable right now. We do need to consider a way of defining placeholder types for optional dependencies so we can type check correctly without these workarounds.

else:
NdarrayOrCupy = Any

__all__ = ["ImageReader", "ITKReader", "NibabelReader", "NumpyReader", "PILReader", "PydicomReader", "NrrdReader"]


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

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

for data_array, metadata in ensure_tuple(dicom_data):
Expand Down Expand Up @@ -1104,10 +1108,7 @@ def get_data(self, img) -> tuple[np.ndarray, dict]:
img: a Nibabel image object loaded from an image file or a list of Nibabel image objects.

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

for i, filename in zip(ensure_tuple(img), self.filenames):
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.