π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix: replace generic exceptions and enable Ruff TRY002 (#5686)
Manu-2717c652c8449a8c
diff --git a/changelog.d/5686.fixed.md b/changelog.d/5686.fixed.mdnew file mode 100644index 000000000..9f42e6504--- /dev/null+++ b/changelog.d/5686.fixed.md@@ -0,0 +1 @@+`augmentation.CenterCrop`, `augmentation.CenterCrop3D` and `geometry.transform.CenterCrop2D` now raise `TypeError` when `size` is neither an int nor a tuple, and DeDoDe's `ConvRefiner` raises `ValueError` when a depthwise block's `out_dim` is not a multiple of its `in_dim`. Both used to raise a bare `Exception`; code that catches `Exception` is unaffected.diff --git a/kornia/augmentation/_2d/geometric/center_crop.py b/kornia/augmentation/_2d/geometric/center_crop.pyindex 6bd2d17eb..e13d52ade 100644--- a/kornia/augmentation/_2d/geometric/center_crop.py+++ b/kornia/augmentation/_2d/geometric/center_crop.py@@ -107,7 +107,7 @@ class CenterCrop(GeometricAugmentationBase2D):elif isinstance(size, int):self.size = (size, size)else:- raise Exception(f"Invalid size type. Expected (int, tuple(int, int). Got: {type(size)}.")+ raise TypeError(f"Invalid size type. Expected int or tuple[int, int]. Got: {type(size)}.")self.flags = {"resample": Resample.get(resample),diff --git a/kornia/augmentation/_3d/geometric/center_crop.py b/kornia/augmentation/_3d/geometric/center_crop.pyindex 2b835ea42..dc5c90f7b 100644--- a/kornia/augmentation/_3d/geometric/center_crop.py+++ b/kornia/augmentation/_3d/geometric/center_crop.py@@ -102,7 +102,7 @@ class CenterCrop3D(GeometricAugmentationBase3D):elif isinstance(size, int):self.size = (size, size, size)else:- raise Exception(f"Invalid size type. Expected (int, tuple(int, int int). Got: {size}.")+ raise TypeError(f"Invalid size type. Expected int or tuple[int, int, int]. Got: {type(size)}.")self.flags = {"align_corners": align_corners, "resample": Resample.get(resample)}def generate_parameters(self, batch_shape: Tuple[int, ...]) -> Dict[str, torch.Tensor]:diff --git a/kornia/feature/dedode/decoder.py b/kornia/feature/dedode/decoder.pyindex b386926da..bb40d29ae 100644--- a/kornia/feature/dedode/decoder.py+++ b/kornia/feature/dedode/decoder.py@@ -132,7 +132,7 @@ class ConvRefiner(nn.Module):"""num_groups = 1 if not dw else in_dimif dw and out_dim % in_dim != 0:- raise Exception("outdim must be divisible by indim for depthwise")+ raise ValueError("out_dim must be divisible by in_dim for depthwise convolution")conv1 = nn.Conv2d(in_dim,out_dim,diff --git a/kornia/geometry/transform/crop2d.py b/kornia/geometry/transform/crop2d.pyindex 33fb133da..7a94fbe98 100644--- a/kornia/geometry/transform/crop2d.py+++ b/kornia/geometry/transform/crop2d.py@@ -644,7 +644,7 @@ class CenterCrop2D(nn.Module):elif isinstance(size, int):self.size = (size, size)else:- raise Exception(f"Invalid size type. Expected (int, tuple(int, int). Got: {type(size)}.")+ raise TypeError(f"Invalid size type. Expected int or tuple[int, int]. Got: {type(size)}.")dst_h, dst_w = self.sizepoints_dst = torch.tensor([[[0, 0], [dst_w - 1, 0], [dst_w - 1, dst_h - 1], [0, dst_h - 1]]], dtype=torch.long)diff --git a/pyproject.toml b/pyproject.tomlindex dab98e377..0941e3216 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -245,6 +245,7 @@ select = ["T10", # flake8-debugger"T201", # flake8-print: print in the library; see the per-file ignore below"TID", # flake8-tidy-imports+ "TRY002", # raise-vanilla-class"UP", # pyupgrade"W", # pycodestyle"YTT", # flake8-2020@@ -279,7 +280,7 @@ select = [# "T20", # flake8-print. Partial: T201 is enabled above for `kornia/` only# "TC", # flake8-type-checking. Partial: TC004 and TC005 are wanted. No: TC001-TC003, TC006# "TD", # flake8-todos. No: the TODO format is not checked- # "TRY", # tryceratops. Partial: TRY002 is wanted. No: TRY003 (as EM), TRY004 (changes exception classes)+ # "TRY", # tryceratops. Partial: TRY002 is enabled above. No: TRY003 (as EM), TRY004 (changes exception classes)]ignore = ["B905", # `zip()` without explicit `strict=` - too many to fix, not a bugdiff --git a/tests/augmentation/_3d/test_center_crop_3d.py b/tests/augmentation/_3d/test_center_crop_3d.pyindex 63dc6a428..8eb3d2b44 100644--- a/tests/augmentation/_3d/test_center_crop_3d.py+++ b/tests/augmentation/_3d/test_center_crop_3d.py@@ -15,6 +15,7 @@# limitations under the License.#+import pytestimport torchfrom kornia.augmentation import CenterCrop3D@@ -23,6 +24,10 @@ from testing.base import BaseTesterclass TestCenterCrop3D(BaseTester):+ def test_invalid_size_type(self):+ with pytest.raises(TypeError, match="Expected int or tuple\\[int, int, int\\]"):+ CenterCrop3D([3, 4, 5])+def test_no_transform(self, device, dtype):inp = torch.rand(1, 2, 4, 4, 4, device=device, dtype=dtype)out = CenterCrop3D(2)(inp)diff --git a/tests/augmentation/test_augmentation.py b/tests/augmentation/test_augmentation.pyindex 4b21c8a33..79616799f 100644--- a/tests/augmentation/test_augmentation.py+++ b/tests/augmentation/test_augmentation.py@@ -3363,6 +3363,10 @@ class TestRandomGrayscale(BaseTester):class TestCenterCrop(BaseTester):+ def test_invalid_size_type(self):+ with pytest.raises(TypeError, match="Expected int or tuple\\[int, int\\]"):+ CenterCrop([4, 6])+def test_dynamo(self, device, dtype, torch_optimizer):# slice cropping mode: a static-size centered crop is fullgraph-safe and matches eager.input = torch.rand(2, 3, 20, 24, device=device, dtype=dtype)diff --git a/tests/feature/test_dedode.py b/tests/feature/test_dedode.pyindex 143714201..330cfbaf4 100644--- a/tests/feature/test_dedode.py+++ b/tests/feature/test_dedode.py@@ -29,6 +29,11 @@ from testing.base import BaseTesterclass TestConvRefiner(BaseTester):+ def test_depthwise_channels_must_be_divisible(self):+ refiner = ConvRefiner(in_dim=4, hidden_dim=4, out_dim=4)+ with pytest.raises(ValueError, match="out_dim must be divisible by in_dim"):+ refiner.create_block(in_dim=3, out_dim=4, dw=True)+def test_amp_matches_input_device(self, device):refiner = ConvRefiner(in_dim=4, hidden_dim=4, out_dim=4).to(device)autocast_enabled = []diff --git a/tests/geometry/transform/test_crop2d.py b/tests/geometry/transform/test_crop2d.pyindex d2bb84f16..4ea687019 100644--- a/tests/geometry/transform/test_crop2d.py+++ b/tests/geometry/transform/test_crop2d.py@@ -208,6 +208,10 @@ class TestCropAndResize(BaseTester):class TestCenterCrop(BaseTester):+ def test_invalid_size_type(self):+ with pytest.raises(TypeError, match="Expected int or tuple\\[int, int\\]"):+ kornia.geometry.transform.CenterCrop2D([4, 6])+def test_center_crop_h2_w4(self, device, dtype):inp = torch.tensor([[[[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0], [9.0, 10.0, 11.0, 12.0], [13.0, 14.0, 15.0, 16.0]]]],