enhance numpy arrays static type checking

Author: onuralpszrCreated Mar 19, 2024Updated Sep 14, 2026
Labelsenhancement

Description

  • This issue aims to improve the static type checking in our codebase and expand the functionality of the Supervision library. The enhancements will make the static type checking and improve code readilibty and help developers to understand which type they need to use
  • Ensure type checking for all possible the functions and classes in the Supervision library.

Old Style code snippet


import numpy as np

@dataclass
class Detection:
    xyxy: np.ndarray
    mask: Optional[np.ndarray] = None
    confidence: Optional[np.ndarray] = None
    class_id: Optional[np.ndarray] = None
    tracker_id: Optional[np.ndarray] = None
    data: Dict[str, Union[np.ndarray, List]] = field(default_factory=dict)

    def __len__(self):
        return len(self.xyxy)

    def __eq__(self, other: Detections):
        return all(
            [
                np.array_equal(self.xyxy, other.xyxy),
                np.array_equal(self.mask, other.mask),
                np.array_equal(self.class_id, other.class_id),
                np.array_equal(self.confidence, other.confidence),
                np.array_equal(self.tracker_id, other.tracker_id),
                is_data_equal(self.data, other.data),
            ]
        )

New Style code snippet


import numpy as np
import numpy.typing as npt

@dataclass
class Detection:
    xyxy: npt.NDArray[np.float32]
    mask: Optional[npt.NDArray[np.float32]] = None
    confidence: Optional[npt.NDArray[np.float32]] = None
    class_id: Optional[npt.NDArray[np.float32]] = None
    tracker_id: Optional[npt.NDArray[np.float32]] = None
    data: Dict[str, Union[npt.NDArray[Any], List[Any]]] = field(default_factory=dict)

    def __len__(self) -> int:
        return len(self.xyxy)

    def __eq__(self, other: Detections) -> bool:
        return all(
            [
                np.array_equal(self.xyxy, other.xyxy),
                np.array_equal(self.mask, other.mask),
                np.array_equal(self.class_id, other.class_id),
                np.array_equal(self.confidence, other.confidence),
                np.array_equal(self.tracker_id, other.tracker_id),
                is_data_equal(self.data, other.data),
            ]
        )

Additional

  • Note: Please share a Google Colab with minimal code to test the new feature. We know it's additional work, but it will definitely speed up the review process. Each change must be tested by the reviewer. Setting up a local environment to do this is time-consuming. Please ensure that Google Colab can be accessed without any issues (make it public). Thank you!