From 4c1f33189b5e4e52132041e644cae5dbd6cf2f16 Mon Sep 17 00:00:00 2001 From: Kader Miyanyedi <48386782+Kadermiyanyedi@users.noreply.github.com> Date: Mon, 2 Feb 2026 09:57:24 +0300 Subject: [PATCH] refactor(typing): Typing error has been fixed for draw and geometry files. (#2120) --- pyproject.toml | 5 ----- supervision/draw/color.py | 9 ++++++--- supervision/draw/utils.py | 22 ++++++++++++++-------- supervision/geometry/core.py | 4 ++-- 4 files changed, 22 insertions(+), 18 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index a6b21c7a..56fa48f9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -232,11 +232,6 @@ module = [ "supervision.detection.utils.masks", "supervision.detection.utils.polygons", "supervision.detection.vlm", - "supervision.draw.base", - "supervision.draw.color", - "supervision.draw.utils", - "supervision.geometry.core", - "supervision.geometry.utils", "supervision.key_points.annotators", "supervision.key_points.core", "supervision.key_points.skeletons", diff --git a/supervision/draw/color.py b/supervision/draw/color.py index 705d5922..a8977b18 100644 --- a/supervision/draw/color.py +++ b/supervision/draw/color.py @@ -1,6 +1,7 @@ from __future__ import annotations from dataclasses import dataclass +from typing import Any, cast import matplotlib.pyplot as plt @@ -53,7 +54,7 @@ LEGACY_COLOR_PALETTE = [ ROBOFLOW_COLOR_PALETTE = ["C28DFC", "A351FB", "8315F9", "6706CE", "5905B3", "4D049A"] -def _validate_color_hex(color_hex: str): +def _validate_color_hex(color_hex: str) -> None: color_hex = color_hex.lstrip("#") if not all(c in "0123456789abcdefABCDEF" for c in color_hex): raise ValueError("Invalid characters in color hash") @@ -259,10 +260,10 @@ class Color: def ROBOFLOW(cls) -> Color: return Color.from_hex("#A351FB") - def __hash__(self): + def __hash__(self) -> int: return hash((self.r, self.g, self.b)) - def __eq__(self, other): + def __eq__(self, other: Any) -> bool: return ( isinstance(other, Color) and self.r == other.r @@ -423,5 +424,7 @@ def unify_to_bgr(color: tuple[int, int, int] | Color) -> tuple[int, int, int]: Tuple[int, int, int]: The color in BGR format as a tuple of three integers. """ if issubclass(type(color), Color): + color = cast(Color, color) return color.as_bgr() + color = cast(tuple[int, int, int], color) return color diff --git a/supervision/draw/utils.py b/supervision/draw/utils.py index 0d9ffe12..6fbeb28b 100644 --- a/supervision/draw/utils.py +++ b/supervision/draw/utils.py @@ -1,6 +1,7 @@ from __future__ import annotations import os +from typing import cast import cv2 import numpy as np @@ -312,26 +313,31 @@ def draw_image( if not 0.0 <= opacity <= 1.0: raise ValueError("Opacity must be between 0.0 and 1.0.") + rect_x = int(rect.x) + rect_y = int(rect.y) + rect_width = int(rect.width) + rect_height = int(rect.height) # Validate rectangle dimensions if ( - rect.x < 0 - or rect.y < 0 - or rect.x + rect.width > scene.shape[1] - or rect.y + rect.height > scene.shape[0] + rect_x < 0 + or rect_y < 0 + or rect_x + rect_width > scene.shape[1] + or rect_y + rect_height > scene.shape[0] ): raise ValueError("Invalid rectangle dimensions.") # Resize and isolate alpha channel - image = cv2.resize(image, (rect.width, rect.height)) + image = cv2.resize(image, (rect_width, rect_height)) + image = cast(np.ndarray, image) alpha_channel = ( image[:, :, 3] if image.shape[2] == 4 - else np.ones((rect.height, rect.width), dtype=image.dtype) * 255 + else np.ones((rect_height, rect_width), dtype=image.dtype) * 255 ) alpha_scaled = cv2.convertScaleAbs(alpha_channel * opacity) # Perform blending - scene_roi = scene[rect.y : rect.y + rect.height, rect.x : rect.x + rect.width] + scene_roi = scene[rect_y : rect_y + rect_height, rect_x : rect_x + rect_width] alpha_float = alpha_scaled.astype(np.float32) / 255.0 blended_roi = cv2.convertScaleAbs( (1 - alpha_float[..., np.newaxis]) * scene_roi @@ -339,7 +345,7 @@ def draw_image( ) # Update the scene - scene[rect.y : rect.y + rect.height, rect.x : rect.x + rect.width] = blended_roi + scene[rect_y : rect_y + rect_height, rect_x : rect_x + rect_width] = blended_roi return scene diff --git a/supervision/geometry/core.py b/supervision/geometry/core.py index bfa18787..ab43265d 100644 --- a/supervision/geometry/core.py +++ b/supervision/geometry/core.py @@ -22,7 +22,7 @@ class Position(Enum): CENTER_OF_MASS = "CENTER_OF_MASS" @classmethod - def list(cls): + def list(cls) -> list[str]: return list(map(lambda c: c.value, cls)) @@ -110,7 +110,7 @@ class Rect: def bottom_right(self) -> Point: return Point(x=self.x + self.width, y=self.y + self.height) - def pad(self, padding) -> Rect: + def pad(self, padding: int) -> Rect: return Rect( x=self.x - padding, y=self.y - padding,