Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

test: enforce Ruff PT012 repository-wide (#5118)

Daniele Loru8d05d5cffeb6
diff --git a/pyproject.toml b/pyproject.toml
index 7a8ada8f2..8673e9380 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -213,6 +213,7 @@ select = [
"PIE", # flake8-pie
"PL", # Pylint
"PT001", # flake8-pytest-style: fixture parentheses
+ "PT012", # flake8-pytest-style: one simple statement in pytest.raises blocks
"PT013", # flake8-pytest-style: pytest imports
"PT014", # flake8-pytest-style: duplicate parametrized cases
"PT017", # flake8-pytest-style: assertions in exception handlers
diff --git a/tests/filters/test_blur_pool.py b/tests/filters/test_blur_pool.py
index 11faccffb..2596e463d 100644
--- a/tests/filters/test_blur_pool.py
+++ b/tests/filters/test_blur_pool.py
@@ -167,12 +167,12 @@ class TestEdgeAwareBlurPool(BaseTester):
def test_exception(self):
from kornia.core.exceptions import BaseError, ShapeError
+ data = torch.rand(1, 3, 3)
with pytest.raises(ShapeError) as errinfo:
- data = torch.rand(1, 3, 3)
edge_aware_blur_pool2d(data, 3)
assert "Shape dimension mismatch" in str(errinfo.value) or "Expected shape" in str(errinfo.value)
+ data = torch.rand(1, 1, 3, 3)
with pytest.raises(BaseError) as errinfo:
- data = torch.rand(1, 1, 3, 3)
edge_aware_blur_pool2d(data, 3, edge_threshold=-1)
assert "edge threshold should be positive, but got" in str(errinfo.value)
diff --git a/tests/filters/test_in_range.py b/tests/filters/test_in_range.py
index eeeb7c090..d6b66a0d0 100644
--- a/tests/filters/test_in_range.py
+++ b/tests/filters/test_in_range.py
@@ -92,19 +92,19 @@ class TestInRange(BaseTester):
):
InRange(lower=(0.2,), upper=(0.2,))(input_tensor)
+ lower = torch.tensor([0.2, 0.2, 0.2])
+ upper = torch.tensor([0.6, 0.6, 0.6])
with pytest.raises(
ValueError,
match=re.escape(
"`lower` and `upper` bounds as Tensors must have compatible shapes with the input (B, C, 1, 1)."
),
):
- lower = torch.tensor([0.2, 0.2, 0.2])
- upper = torch.tensor([0.6, 0.6, 0.6])
InRange(lower=lower, upper=upper)(input_tensor)
+ lower = torch.tensor([0.2, 0.2, 0.2])
+ upper = torch.tensor([0.6, 0.6, 0.6])
with pytest.raises(Exception, match=r"Invalid `return_mask` format. Should be boolean."):
- lower = torch.tensor([0.2, 0.2, 0.2])
- upper = torch.tensor([0.6, 0.6, 0.6])
InRange(lower=lower, upper=upper, return_mask=2)(input_tensor)
def test_tensor_bounds_return_masked_input(self, device, dtype):
diff --git a/tests/image/test_print.py b/tests/image/test_print.py
index 8edefed79..aead4eba7 100644
--- a/tests/image/test_print.py
+++ b/tests/image/test_print.py
@@ -43,15 +43,15 @@ class TestImageToString:
from kornia.core.exceptions import ShapeError
+ img = torch.rand(1, 3, 15, 15)
with pytest.raises(ShapeError) as errinfo:
- img = torch.rand(1, 3, 15, 15)
image_to_string(img)
assert "Shape dimension mismatch" in str(errinfo.value) or "Expected shape" in str(errinfo.value)
from kornia.core.exceptions import ValueCheckError
+ img = torch.rand(3, 15, 15) * 10
with pytest.raises(ValueCheckError) as errinfo:
- img = torch.rand(3, 15, 15) * 10
image_to_string(img)
assert "Value range mismatch" in str(errinfo.value) or "Invalid image value range" in str(errinfo.value)
diff --git a/tests/metrics/test_aepe.py b/tests/metrics/test_aepe.py
index 1e1a2079d..d2305a368 100644
--- a/tests/metrics/test_aepe.py
+++ b/tests/metrics/test_aepe.py
@@ -60,20 +60,20 @@ class TestAepe(BaseTester):
def test_exception(self, device, dtype):
from kornia.core.exceptions import TypeCheckError
+ criterion = kornia.metrics.AEPE()
with pytest.raises(TypeCheckError) as errinfo:
- criterion = kornia.metrics.AEPE()
criterion(None, torch.ones(4, 4, 2, device=device, dtype=dtype))
assert "Type mismatch: expected Tensor" in str(errinfo.value)
+ sample = torch.ones(4, 4, 2, device=device, dtype=dtype)
with pytest.raises(NotImplementedError) as errinfo:
- sample = torch.ones(4, 4, 2, device=device, dtype=dtype)
_ = kornia.metrics.aepe(sample, 2.0 * sample, reduction="foo")
assert "Invalid reduction option." in str(errinfo)
from kornia.core.exceptions import ShapeError
+ sample = torch.ones(4, 4, 2, device=device, dtype=dtype)
with pytest.raises(ShapeError) as errinfo:
- sample = torch.ones(4, 4, 2, device=device, dtype=dtype)
_ = kornia.metrics.aepe(sample, 2.0 * sample[..., 0], reduction="mean")
assert (
"Shape dimension mismatch" in str(errinfo.value)
diff --git a/tests/metrics/test_disparity.py b/tests/metrics/test_disparity.py
index 54ef345fe..17a3ef8e2 100644
--- a/tests/metrics/test_disparity.py
+++ b/tests/metrics/test_disparity.py
@@ -226,8 +226,8 @@ class TestMeanAbsoluteDisparityError(BaseTester):
kornia.metrics.mean_absolute_disparity_error(sample, sample[..., :2])
assert "Shape mismatch" in str(errinfo.value)
+ mask = torch.ones(3, device=device, dtype=torch.bool)
with pytest.raises(BaseError) as errinfo:
- mask = torch.ones(3, device=device, dtype=torch.bool)
kornia.metrics.mean_absolute_disparity_error(sample, sample, mask)
assert "broadcastable" in str(errinfo.value)
@@ -564,8 +564,8 @@ class TestKittiD1Error(BaseTester):
kornia.metrics.kitti_d1_error(sample, sample[..., :2])
assert "Shape mismatch" in str(errinfo.value)
+ mask = torch.ones(3, device=device, dtype=torch.bool)
with pytest.raises(BaseError) as errinfo:
- mask = torch.ones(3, device=device, dtype=torch.bool)
kornia.metrics.kitti_d1_error(sample, sample, valid_mask=mask)
assert "broadcastable" in str(errinfo.value)
diff --git a/tests/models/test_kimi_vl.py b/tests/models/test_kimi_vl.py
index fb9d18a34..dd2c49ba4 100644
--- a/tests/models/test_kimi_vl.py
+++ b/tests/models/test_kimi_vl.py
@@ -169,8 +169,8 @@ class TestKimiVLModel(BaseTester):
def test_exception(self, device, dtype, model):
# Test invalid input shape (missing batch dim)
+ images = torch.randn(3, 32, 32, device=device, dtype=dtype)
with pytest.raises((RuntimeError, ValueError, IndexError)):
- images = torch.randn(3, 32, 32, device=device, dtype=dtype)
model(images)
def test_gradcheck(self, device, config):
diff --git a/tests/models/test_sam.py b/tests/models/test_sam.py
index 653fcb6c3..a3a28ca43 100644
--- a/tests/models/test_sam.py
+++ b/tests/models/test_sam.py
@@ -70,13 +70,13 @@ class TestSam(BaseTester):
from kornia.core.exceptions import ShapeError
+ data = torch.rand(3, 1, 2)
with pytest.raises(ShapeError) as errinfo:
- data = torch.rand(3, 1, 2)
model(data, [], False)
assert "Shape dimension mismatch" in str(errinfo.value) or "Expected shape" in str(errinfo.value)
+ data = torch.rand(2, 3, 1, 2)
with pytest.raises(Exception) as errinfo:
- data = torch.rand(2, 3, 1, 2)
model(data, [{}], False)
assert "The number of images (`B`) should match with the length of prompts!" in str(errinfo)
diff --git a/tests/models/test_siglip2.py b/tests/models/test_siglip2.py
index c8a491304..05c28b66a 100644
--- a/tests/models/test_siglip2.py
+++ b/tests/models/test_siglip2.py
@@ -168,20 +168,20 @@ class TestSigLip2Model(BaseTester):
def test_exception(self, device, dtype, model, config):
"""Test exception handling."""
# Test invalid pixel_values shape (wrong number of dimensions)
+ image_size = config.vision_config.image_size
+ invalid_pixel_values = torch.randn(3, image_size, image_size, device=device, dtype=dtype)
with pytest.raises((RuntimeError, ValueError, IndexError)):
- image_size = config.vision_config.image_size
- invalid_pixel_values = torch.randn(3, image_size, image_size, device=device, dtype=dtype)
model.get_image_features(invalid_pixel_values)
# Test invalid attention mask shape
+ input_ids = _create_input_ids(2, 10, config, device)
+ invalid_attention_mask = torch.ones(2, 5, device=device) # Wrong sequence length
with pytest.raises((RuntimeError, ValueError, IndexError)):
- input_ids = _create_input_ids(2, 10, config, device)
- invalid_attention_mask = torch.ones(2, 5, device=device) # Wrong sequence length
model.get_text_features(input_ids, attention_mask=invalid_attention_mask)
# Test input_ids with wrong number of dimensions
+ invalid_input_ids = torch.randint(0, 100, (10,), device=device) # Missing batch dimension
with pytest.raises((RuntimeError, ValueError, IndexError)):
- invalid_input_ids = torch.randint(0, 100, (10,), device=device) # Missing batch dimension
model.get_text_features(invalid_input_ids)
def test_get_image_features(self, device, dtype, model, config):
diff --git a/tests/models/test_small_sr.py b/tests/models/test_small_sr.py
index 1c3c5f0fb..aa089fd80 100644
--- a/tests/models/test_small_sr.py
+++ b/tests/models/test_small_sr.py
@@ -46,8 +46,8 @@ class TestSmallSRNet(BaseTester):
model = SmallSRNet(upscale_factor=3, pretrained=False).to(device, dtype)
# SmallSRNet expects 1 channel input (Y channel), not 3
+ x = torch.randn(1, 3, 16, 16, device=device, dtype=dtype)
with pytest.raises(RuntimeError):
- x = torch.randn(1, 3, 16, 16, device=device, dtype=dtype)
model(x)
def test_smoke_upscale_factor_one(self, device, dtype):
@@ -164,8 +164,8 @@ class TestSmallSRNetWrapper(BaseTester):
model = SmallSRNetWrapper(upscale_factor=3, pretrained=False).to(device, dtype)
# SmallSRNetWrapper expects 3 channel RGB input, rgb_to_ycbcr will raise ValueError
+ x = torch.randn(1, 1, 16, 16, device=device, dtype=dtype)
with pytest.raises(ValueError, match="Input size must have a shape of"):
- x = torch.randn(1, 1, 16, 16, device=device, dtype=dtype)
model(x)
def test_exception_negative_values(self, device, dtype):
diff --git a/tests/morphology/test_bottom_hat.py b/tests/morphology/test_bottom_hat.py
index 1266f91a7..004e37d42 100644
--- a/tests/morphology/test_bottom_hat.py
+++ b/tests/morphology/test_bottom_hat.py
@@ -103,12 +103,12 @@ class TestBottomHat(BaseTester):
with pytest.raises(TypeError):
assert bottom_hat(sample, [0.0])
+ test = torch.ones(2, 3, 4, device=device, dtype=dtype)
with pytest.raises(ValueError):
- test = torch.ones(2, 3, 4, device=device, dtype=dtype)
assert bottom_hat(test, kernel)
+ test = torch.ones(2, 3, 4, device=device, dtype=dtype)
with pytest.raises(ValueError):
- test = torch.ones(2, 3, 4, device=device, dtype=dtype)
assert bottom_hat(sample, test)
def test_jit(self, device, dtype):
diff --git a/tests/morphology/test_closing.py b/tests/morphology/test_closing.py
index 685763d8b..71c867240 100644
--- a/tests/morphology/test_closing.py
+++ b/tests/morphology/test_closing.py
@@ -103,12 +103,12 @@ class TestClosing(BaseTester):
with pytest.raises(TypeError):
assert closing(tensor, [0.0])
+ test = torch.ones(2, 3, 4, device=device, dtype=dtype)
with pytest.raises(ValueError):
- test = torch.ones(2, 3, 4, device=device, dtype=dtype)
assert closing(test, kernel)
+ test = torch.ones(2, 3, 4, device=device, dtype=dtype)
with pytest.raises(ValueError):
- test = torch.ones(2, 3, 4, device=device, dtype=dtype)
assert closing(tensor, test)
def test_jit(self, device, dtype):
diff --git a/tests/morphology/test_dilation.py b/tests/morphology/test_dilation.py
index df5d2c49e..106cb8c98 100644
--- a/tests/morphology/test_dilation.py
+++ b/tests/morphology/test_dilation.py
@@ -133,12 +133,12 @@ class TestDilate(BaseTester):
with pytest.raises(TypeError):
assert dilation(tensor, [0.0])
+ test = torch.ones(2, 3, 4, device=device, dtype=dtype)
with pytest.raises(ValueError):
- test = torch.ones(2, 3, 4, device=device, dtype=dtype)
assert dilation(test, kernel)
+ test = torch.ones(2, 3, 4, device=device, dtype=dtype)
with pytest.raises(ValueError):
- test = torch.ones(2, 3, 4, device=device, dtype=dtype)
assert dilation(tensor, test)
with pytest.raises(NotImplementedError, match="unknown"):
diff --git a/tests/morphology/test_erosion.py b/tests/morphology/test_erosion.py
index 3d3af50c7..7c558a90a 100644
--- a/tests/morphology/test_erosion.py
+++ b/tests/morphology/test_erosion.py
@@ -133,12 +133,12 @@ class TestErode(BaseTester):
with pytest.raises(TypeError):
assert erosion(tensor, [0.0])
+ test = torch.ones(2, 3, 4, device=device, dtype=dtype)
with pytest.raises(ValueError):
- test = torch.ones(2, 3, 4, device=device, dtype=dtype)
assert erosion(test, kernel)
+ test = torch.ones(2, 3, 4, device=device, dtype=dtype)
with pytest.raises(ValueError):
- test = torch.ones(2, 3, 4, device=device, dtype=dtype)
assert erosion(tensor, test)
with pytest.raises(ValueError, match="Unknown `border_type`"):
diff --git a/tests/morphology/test_gradient.py b/tests/morphology/test_gradient.py
index e51867864..ce32f2b60 100644
--- a/tests/morphology/test_gradient.py
+++ b/tests/morphology/test_gradient.py
@@ -103,12 +103,12 @@ class TestGradient(BaseTester):
with pytest.raises(TypeError):
assert gradient(tensor, [0.0])
+ test = torch.ones(2, 3, 4, device=device, dtype=dtype)
with pytest.raises(ValueError):
- test = torch.ones(2, 3, 4, device=device, dtype=dtype)
assert gradient(test, kernel)
+ test = torch.ones(2, 3, 4, device=device, dtype=dtype)
with pytest.raises(ValueError):
- test = torch.ones(2, 3, 4, device=device, dtype=dtype)
assert gradient(tensor, test)
def test_jit(self, device, dtype):
diff --git a/tests/morphology/test_opening.py b/tests/morphology/test_opening.py
index c845b77f3..c642eeaef 100644
--- a/tests/morphology/test_opening.py
+++ b/tests/morphology/test_opening.py
@@ -103,12 +103,12 @@ class TestOpening(BaseTester):
with pytest.raises(TypeError):
assert opening(tensor, [0.0])
+ test = torch.ones(2, 3, 4, device=device, dtype=dtype)
with pytest.raises(ValueError):
- test = torch.ones(2, 3, 4, device=device, dtype=dtype)
assert opening(test, kernel)
+ test = torch.ones(2, 3, 4, device=device, dtype=dtype)
with pytest.raises(ValueError):
- test = torch.ones(2, 3, 4, device=device, dtype=dtype)
assert opening(tensor, test)
def test_jit(self, device, dtype):
diff --git a/tests/morphology/test_top_hat.py b/tests/morphology/test_top_hat.py
index 2c47b32a7..b9bf8c691 100644
--- a/tests/morphology/test_top_hat.py
+++ b/tests/morphology/test_top_hat.py
@@ -103,12 +103,12 @@ class TestTopHat(BaseTester):
with pytest.raises(TypeError):
assert top_hat(sample, [0.0])
+ test = torch.ones(2, 3, 4, device=device, dtype=dtype)
with pytest.raises(ValueError):
- test = torch.ones(2, 3, 4, device=device, dtype=dtype)
assert top_hat(test, kernel)
+ test = torch.ones(2, 3, 4, device=device, dtype=dtype)
with pytest.raises(ValueError):
- test = torch.ones(2, 3, 4, device=device, dtype=dtype)
assert top_hat(sample, test)
def test_jit(self, device, dtype):