Skip to content

Commit 89c50fe

Browse files
committed
refactor: replace List and Tuple with built-in list and tuple type hints
1 parent 8f9bfe9 commit 89c50fe

File tree

4 files changed

+32
-43
lines changed

4 files changed

+32
-43
lines changed

bbox_visualizer/core/_utils.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
import logging
44
from contextlib import contextmanager
5-
from typing import List, Tuple
65

76
# Global flag to track warning suppression state
8-
_warnings_suppressed = False
7+
_warnings_suppressed: bool = False
98

109
def suppress_warnings(suppress: bool = True) -> None:
1110
"""Suppress or enable warning messages from bbox-visualizer.
@@ -36,10 +35,7 @@ def _should_suppress_warning() -> bool:
3635
"""Internal function to check if warnings should be suppressed."""
3736
return _warnings_suppressed
3837

39-
from typing import List, Tuple
40-
41-
42-
def _validate_bbox(bbox: List[int]) -> None:
38+
def _validate_bbox(bbox: list[int]) -> None:
4339
"""Validate bounding box format and values.
4440
4541
Args:
@@ -59,8 +55,7 @@ def _validate_bbox(bbox: List[int]) -> None:
5955
"Invalid bounding box coordinates: x_min > x_max or y_min > y_max"
6056
)
6157

62-
63-
def _validate_color(color: Tuple[int, int, int]) -> None:
58+
def _validate_color(color: tuple[int, int, int]) -> None:
6459
"""Validate BGR color values.
6560
6661
Args:
@@ -72,10 +67,9 @@ def _validate_color(color: Tuple[int, int, int]) -> None:
7267
if not all(0 <= c <= 255 for c in color):
7368
raise ValueError("Color values must be between 0 and 255")
7469

75-
7670
def _check_and_modify_bbox(
77-
bbox: List[int], img_size: Tuple[int, int, int], margin: int = 0
78-
) -> List[int]:
71+
bbox: list[int], img_size: tuple[int, int, int], margin: int = 0
72+
) -> list[int]:
7973
"""Internal function to check and adjust bounding box coordinates.
8074
8175
.. private::

bbox_visualizer/core/flags.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Functions for drawing flag and T-shaped labels."""
22

33
import logging
4-
from typing import List, Tuple
54

65
import cv2
76
import numpy as np
@@ -16,12 +15,12 @@
1615
def add_T_label(
1716
img: np.ndarray,
1817
label: str,
19-
bbox: List[int],
18+
bbox: list[int],
2019
size: float = 1,
2120
thickness: int = 2,
2221
draw_bg: bool = True,
23-
text_bg_color: Tuple[int, int, int] = (255, 255, 255),
24-
text_color: Tuple[int, int, int] = (0, 0, 0),
22+
text_bg_color: tuple[int, int, int] = (255, 255, 255),
23+
text_color: tuple[int, int, int] = (0, 0, 0),
2524
) -> np.ndarray:
2625
"""Adds a T-shaped label with a vertical line connecting to the bounding box.
2726
@@ -100,13 +99,13 @@ def add_T_label(
10099
def draw_flag_with_label(
101100
img: np.ndarray,
102101
label: str,
103-
bbox: List[int],
102+
bbox: list[int],
104103
size: float = 1,
105104
thickness: int = 2,
106105
write_label: bool = True,
107-
line_color: Tuple[int, int, int] = (255, 255, 255),
108-
text_bg_color: Tuple[int, int, int] = (255, 255, 255),
109-
text_color: Tuple[int, int, int] = (0, 0, 0),
106+
line_color: tuple[int, int, int] = (255, 255, 255),
107+
text_bg_color: tuple[int, int, int] = (255, 255, 255),
108+
text_color: tuple[int, int, int] = (0, 0, 0),
110109
) -> np.ndarray:
111110
"""Draws a flag-like label with a vertical line and text box.
112111
@@ -183,11 +182,11 @@ def draw_flag_with_label(
183182

184183
def add_multiple_T_labels(
185184
img: np.ndarray,
186-
labels: List[str],
187-
bboxes: List[List[int]],
185+
labels: list[str],
186+
bboxes: list[list[int]],
188187
draw_bg: bool = True,
189-
text_bg_color: Tuple[int, int, int] = (255, 255, 255),
190-
text_color: Tuple[int, int, int] = (0, 0, 0),
188+
text_bg_color: tuple[int, int, int] = (255, 255, 255),
189+
text_color: tuple[int, int, int] = (0, 0, 0),
191190
) -> np.ndarray:
192191
"""Adds multiple T-shaped labels to their corresponding bounding boxes.
193192
@@ -226,12 +225,12 @@ def add_multiple_T_labels(
226225

227226
def draw_multiple_flags_with_labels(
228227
img: np.ndarray,
229-
labels: List[str],
230-
bboxes: List[List[int]],
228+
labels: list[str],
229+
bboxes: list[list[int]],
231230
write_label: bool = True,
232-
line_color: Tuple[int, int, int] = (255, 255, 255),
233-
text_bg_color: Tuple[int, int, int] = (255, 255, 255),
234-
text_color: Tuple[int, int, int] = (0, 0, 0),
231+
line_color: tuple[int, int, int] = (255, 255, 255),
232+
text_bg_color: tuple[int, int, int] = (255, 255, 255),
233+
text_color: tuple[int, int, int] = (0, 0, 0),
235234
) -> np.ndarray:
236235
"""Adds multiple flag-like labels to their corresponding bounding boxes.
237236

bbox_visualizer/core/labels.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
"""Functions for adding text labels to bounding boxes."""
22

3-
from typing import List, Tuple
4-
53
import cv2
64
import numpy as np
75

@@ -13,12 +11,12 @@
1311
def add_label(
1412
img: np.ndarray,
1513
label: str,
16-
bbox: List[int],
14+
bbox: list[int],
1715
size: float = 1,
1816
thickness: int = 2,
1917
draw_bg: bool = True,
20-
text_bg_color: Tuple[int, int, int] = (255, 255, 255),
21-
text_color: Tuple[int, int, int] = (0, 0, 0),
18+
text_bg_color: tuple[int, int, int] = (255, 255, 255),
19+
text_color: tuple[int, int, int] = (0, 0, 0),
2220
top: bool = True,
2321
) -> np.ndarray:
2422
"""Add a label to a bounding box, either above or inside it.
@@ -118,13 +116,13 @@ def add_label(
118116

119117
def add_multiple_labels(
120118
img: np.ndarray,
121-
labels: List[str],
122-
bboxes: List[List[int]],
119+
labels: list[str],
120+
bboxes: list[list[int]],
123121
size: float = 1,
124122
thickness: int = 2,
125123
draw_bg: bool = True,
126-
text_bg_color: Tuple[int, int, int] = (255, 255, 255),
127-
text_color: Tuple[int, int, int] = (0, 0, 0),
124+
text_bg_color: tuple[int, int, int] = (255, 255, 255),
125+
text_color: tuple[int, int, int] = (0, 0, 0),
128126
top: bool = True,
129127
) -> np.ndarray:
130128
"""Add multiple labels to their corresponding bounding boxes.

bbox_visualizer/core/rectangle.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
"""Functions for drawing rectangles on images."""
22

3-
from typing import List, Tuple
4-
53
import cv2
64
import numpy as np
75

@@ -10,8 +8,8 @@
108

119
def draw_rectangle(
1210
img: np.ndarray,
13-
bbox: List[int],
14-
bbox_color: Tuple[int, int, int] = (255, 255, 255),
11+
bbox: list[int],
12+
bbox_color: tuple[int, int, int] = (255, 255, 255),
1513
thickness: int = 3,
1614
is_opaque: bool = False,
1715
alpha: float = 0.5,
@@ -47,8 +45,8 @@ def draw_rectangle(
4745

4846
def draw_multiple_rectangles(
4947
img: np.ndarray,
50-
bboxes: List[List[int]],
51-
bbox_color: Tuple[int, int, int] = (255, 255, 255),
48+
bboxes: list[list[int]],
49+
bbox_color: tuple[int, int, int] = (255, 255, 255),
5250
thickness: int = 3,
5351
is_opaque: bool = False,
5452
alpha: float = 0.5,

0 commit comments

Comments
 (0)