Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

test: enforce Ruff PT001, PT022, PT023, and PT030 (#4899)

Daniele Lorud3bda341122b
diff --git a/conftest.py b/conftest.py
index a8b07fe32..2c73351b9 100644
--- a/conftest.py
+++ b/conftest.py
@@ -102,7 +102,7 @@ TEST_OPTIMIZER_BACKEND = {"", None, "jit", *_backends_non_experimental}
DEVICE_DTYPE_BLACKLIST: set[tuple[str, ...]] = set()
def device(device_name) -> torch.device:
"""Return device for testing, skipping if device is unavailable."""
if device_name not in TEST_DEVICES:
@@ -110,7 +110,7 @@ def device(device_name) -> torch.device:
return TEST_DEVICES[device_name]
def restore_torch_rng():
"""Keep explicitly opted-in tests from shifting later CPU/CUDA/MPS random draws (#4446)."""
cpu_state = torch.random.get_rng_state()
@@ -127,13 +127,13 @@ def restore_torch_rng():
torch.mps.set_rng_state(mps_state)
def dtype(dtype_name) -> torch.dtype:
"""Return dtype for testing."""
return TEST_DTYPES[dtype_name]
def torch_optimizer(optimizer_backend):
"""Return torch optimizer based on backend selection.
@@ -153,7 +153,7 @@ def torch_optimizer(optimizer_backend):
return partial(torch.compile, backend=optimizer_backend)
def cudnn_tf32_follows_option(request):
"""Compute convolutions in real float32 on CUDA, so a float32 tolerance means float32.
@@ -883,7 +883,7 @@ def _isolated_test_rng(seed: int):
torch.random.set_rng_state(torch_state)
def test_rng_seed(request) -> int:
"""Return the stable seed for this pytest node without changing global RNG state."""
return seed_test_rng(request.node.nodeid)
diff --git a/pyproject.toml b/pyproject.toml
index 1a994527c..86318f2c8 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -212,9 +212,13 @@ select = [
"NPY", # NumPy-specific rules
"PIE", # flake8-pie
"PL", # Pylint
+ "PT001", # flake8-pytest-style: fixture parentheses
"PT013", # flake8-pytest-style: pytest imports
"PT014", # flake8-pytest-style: duplicate parametrized cases
"PT017", # flake8-pytest-style: assertions in exception handlers
+ "PT022", # flake8-pytest-style: useless yield fixtures
+ "PT023", # flake8-pytest-style: mark parentheses
+ "PT030", # flake8-pytest-style: specific warning matches
"PYI", # flake8-pyi
"RET", # flake8-return
"RSE", # flake8-raise
diff --git a/tests/augmentation/test_conventions_intensity_ops.py b/tests/augmentation/test_conventions_intensity_ops.py
index b7da0e4d5..c7762475f 100644
--- a/tests/augmentation/test_conventions_intensity_ops.py
+++ b/tests/augmentation/test_conventions_intensity_ops.py
@@ -44,7 +44,7 @@ from testing.base import (
@pytest.fixture(autouse=True)
def _restore_global_rng(restore_torch_rng):
# The pins below seed the global RNG; the root fixture (#4446) restores every generator afterwards.
- yield
+ return
# The seed drawn immediately before each construct-and-forward.
diff --git a/tests/augmentation/test_conventions_intensity_values.py b/tests/augmentation/test_conventions_intensity_values.py
index 8f8ee010d..104ca955d 100644
--- a/tests/augmentation/test_conventions_intensity_values.py
+++ b/tests/augmentation/test_conventions_intensity_values.py
@@ -46,7 +46,7 @@ def _restore_global_rng(restore_torch_rng):
# reseeds the CUDA and MPS generators, so the root fixture (#4446) snapshots and restores all of
# them; ``fork_rng(devices=[])`` would restore the CPU generator only and shift the draw of an
# unseeded later test on an accelerator leg.
- yield
+ return
# One constructor per 2D intensity class for the value-range pins below. RandomDissolving (its
diff --git a/tests/enhance/test_equalization.py b/tests/enhance/test_equalization.py
index d634e9f9d..fcebc8731 100644
--- a/tests/enhance/test_equalization.py
+++ b/tests/enhance/test_equalization.py
@@ -216,7 +216,7 @@ class TestEqualization(BaseTester):
with pytest.raises(RuntimeError, match=r"equalize_clahe expects input values in \[0, 1\]"):
_sync(compiled(x).device)
- @pytest.fixture()
+ @pytest.fixture
def img(self, device, dtype):
height, width = 20, 20
# TODO: test with a more realistic pattern
diff --git a/tests/feature/test_integrated.py b/tests/feature/test_integrated.py
index 78e501df5..fb2c4f74f 100644
--- a/tests/feature/test_integrated.py
+++ b/tests/feature/test_integrated.py
@@ -104,7 +104,7 @@ class TestGetLAFDescriptorsEmptyPath(BaseTester):
torch.full((1, 1, 1, 1), 8.0, device=device, dtype=dtype),
)
non_empty = kornia.feature.get_laf_descriptors(img, laf, desc, 32)
- with pytest.warns(UserWarning):
+ with pytest.warns(UserWarning, match=r"LAF contains no keypoints"):
empty = kornia.feature.get_laf_descriptors(
img, torch.zeros(1, 0, 2, 3, device=device, dtype=dtype), desc, 32
)
@@ -141,7 +141,7 @@ class TestGetLAFDescriptorsEmptyPath(BaseTester):
torch.full((1, 1, 1, 1), 8.0, device=device, dtype=dtype),
)
non_empty = kornia.feature.get_laf_descriptors(img, laf, desc, 32)
- with pytest.warns(UserWarning):
+ with pytest.warns(UserWarning, match=r"LAF contains no keypoints"):
empty = kornia.feature.get_laf_descriptors(
img, torch.zeros(1, 0, 2, 3, device=device, dtype=dtype), desc, 32
)
diff --git a/tests/feature/test_laf.py b/tests/feature/test_laf.py
index 0118e2a0b..8c358672a 100644
--- a/tests/feature/test_laf.py
+++ b/tests/feature/test_laf.py
@@ -426,7 +426,7 @@ class TestELL2LAF(BaseTester):
inp[..., 4] += 1.0
return inp
- @pytest.mark.jit()
+ @pytest.mark.jit
def test_jit(self, device, dtype):
batch_size, channels, height = 1, 2, 5
img = torch.rand(batch_size, channels, height, device=device).abs()
diff --git a/tests/geometry/epipolar/test_fundamental.py b/tests/geometry/epipolar/test_fundamental.py
index 6a155f592..8fa17dec2 100644
--- a/tests/geometry/epipolar/test_fundamental.py
+++ b/tests/geometry/epipolar/test_fundamental.py
@@ -276,7 +276,7 @@ class TestFindFundamental(BaseTester):
error = epi.sampson_epipolar_distance(x1, x2, F)
self.assert_close(error, torch.zeros((F.shape[0], 7), device=device, dtype=dtype), atol=1e-4, rtol=1e-4)
- @pytest.mark.xfail()
+ @pytest.mark.xfail
def test_epipolar_constraint_7point(self, device, dtype):
scene: Dict[str, torch.Tensor] = generate_two_view_random_scene(device, dtype)
x1 = scene["x1"][:, :7, :]
diff --git a/tests/geometry/subpix/test_dsnt.py b/tests/geometry/subpix/test_dsnt.py
index 9bec351f3..2280b9506 100644
--- a/tests/geometry/subpix/test_dsnt.py
+++ b/tests/geometry/subpix/test_dsnt.py
@@ -24,7 +24,7 @@ from testing.base import BaseTester
class TestRenderGaussian2d(BaseTester):
- @pytest.fixture()
+ @pytest.fixture
def gaussian(self, device, dtype):
# For a standard gaussian on 5 points [-1, -0.5, 0, 0.5, 1] with std=0.25
# The equation is exp( -x^2 / (2 * std^2) ) -> exp( -x^2 * 8 )
diff --git a/tests/geometry/test_conversions.py b/tests/geometry/test_conversions.py
index 3cb167f23..6dbb6e24f 100644
--- a/tests/geometry/test_conversions.py
+++ b/tests/geometry/test_conversions.py
@@ -62,7 +62,7 @@ def seed_rng() -> None:
torch.manual_seed(0)
def atol(device, dtype):
"""Lower tolerance for cuda-float16 only."""
if "cuda" in device.type and dtype == torch.float16:
@@ -70,7 +70,7 @@ def atol(device, dtype):
return 1.0e-4
def rtol(device, dtype):
"""Lower tolerance for cuda-float16 only."""
if "cuda" in device.type and dtype == torch.float16:
diff --git a/tests/integration/test_conversions.py b/tests/integration/test_conversions.py
index c3e520dad..99df20ae8 100644
--- a/tests/integration/test_conversions.py
+++ b/tests/integration/test_conversions.py
@@ -24,7 +24,7 @@ import kornia
from testing.base import BaseTester
def atol(device, dtype):
"""Lower tolerance for cuda-float16 only."""
if "cuda" in device.type and dtype == torch.float16:
@@ -32,7 +32,7 @@ def atol(device, dtype):
return 1.0e-4
def rtol(device, dtype):
"""Lower tolerance for cuda-float16 only."""
if "cuda" in device.type and dtype == torch.float16:
diff --git a/tests/utils/test_deprecated.py b/tests/utils/test_deprecated.py
index 05bcb5e98..86f9bf772 100644
--- a/tests/utils/test_deprecated.py
+++ b/tests/utils/test_deprecated.py
@@ -210,7 +210,7 @@ class TestRestoredWrappers:
assert isinstance(utils.ImageToTensor, type)
assert issubclass(utils.ImageToTensor, NewImageToTensor)
- with pytest.warns(DeprecationWarning):
+ with pytest.warns(DeprecationWarning, match=r"kornia\.image\.ImageToTensor"):
inst = utils.ImageToTensor()
assert isinstance(inst, NewImageToTensor)