Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(geometry): improve error handling in Boxes.translate for unknown methods (#4988)

Prateekshit Jaiswal333790339bf7
diff --git a/changelog.d/4988.fixed.md b/changelog.d/4988.fixed.md
new file mode 100644
index 000000000..72ba0fdd0
--- /dev/null
+++ b/changelog.d/4988.fixed.md
@@ -0,0 +1,2 @@
+`Boxes.translate` with an unknown `method` now raises `NotImplementedError` with a message that names the accepted
+values and repeats the rejected one, instead of an empty message. The exception type is unchanged.
diff --git a/kornia/geometry/boxes.py b/kornia/geometry/boxes.py
index 1ea291144..5e586caa3 100644
--- a/kornia/geometry/boxes.py
+++ b/kornia/geometry/boxes.py
@@ -941,7 +941,10 @@ class Boxes:
"`Boxes.translate(method='fast')` is not implemented; use `method='warp'` instead."
)
if method != "warp":
- raise NotImplementedError
+ raise NotImplementedError(
+ "`Boxes.translate` accepts `method='warp'` or `method='fast'` (not implemented); "
+ f"got method={method!r}."
+ )
M: torch.Tensor = eye_like(3, size)
M[:, :2, 2] = size
diff --git a/tests/geometry/test_boxes.py b/tests/geometry/test_boxes.py
index 38d1d9ec4..5eab3c2ab 100644
--- a/tests/geometry/test_boxes.py
+++ b/tests/geometry/test_boxes.py
@@ -811,6 +811,12 @@ class TestBoxes2D(BaseTester):
self.assert_close(translated.data, expected, atol=0.0, rtol=0.0)
assert translated is not boxes
+ def test_translate_unknown_method_raises(self, device, dtype):
+ # The message names the accepted methods and repeats the rejected one, so a typo is visible.
+ boxes = Boxes.from_tensor(torch.tensor([[[1.0, 2.0, 5.0, 4.0]]], device=device, dtype=dtype), mode="xyxy")
+ with pytest.raises(NotImplementedError, match=r"method='warp'.*method='fast'.*got method='wrap'"):
+ boxes.translate(torch.tensor([[1.0, 2.0]], device=device, dtype=dtype), method="wrap")
+
def test_wart_filter_boxes_by_area_zeroes_small_boxes_4010(self, device, dtype):
# Wart pin for kornia#4010: filtering acts on compute_area, so the
# valid two-by-one box with shoelace area zero is zeroed, not removed.