Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

style: fix and enforce ruff T201 (print) in the library (#5676)

Daisy DAI53eda086a4dc
diff --git a/changelog.d/5676.fixed.md b/changelog.d/5676.fixed.md
new file mode 100644
index 000000000..2d6122a8b
--- /dev/null
+++ b/changelog.d/5676.fixed.md
@@ -0,0 +1,3 @@
+`LightGlue` no longer prints `Loaded LightGlue model` every time it is constructed, and `match_adalam` (also used by
+`GeometryAwareDescriptorMatcher("adalam", ...)`) reports a configuration key it does not recognize with a
+`UserWarning` instead of printing a `WARNING:` line to stdout; the key is still skipped.
diff --git a/kornia/feature/adalam/adalam.py b/kornia/feature/adalam/adalam.py
index 01f930349..d2e896c4e 100644
--- a/kornia/feature/adalam/adalam.py
+++ b/kornia/feature/adalam/adalam.py
@@ -19,6 +19,7 @@
# https://github.com/cavalli1234/AdaLAM
# Copyright (c) 2020, Luca Cavalli
+import warnings
from typing import Optional, Tuple, Union
import torch
@@ -92,9 +93,10 @@ def match_adalam(
config_ = get_adalam_default_config()
for key, val in config.items():
if key not in config_:
- print(
- f"WARNING: custom configuration contains a key which is not recognized ({key}). "
- f"Known configurations are {list(config_.keys())}."
+ warnings.warn(
+ f"Custom configuration contains a key which is not recognized ({key}). "
+ f"Known configurations are {list(config_.keys())}.",
+ stacklevel=2,
)
continue
# TypedDict does not support variable names. https://stackoverflow.com/a/59583427/1983544
diff --git a/kornia/feature/lightglue.py b/kornia/feature/lightglue.py
index d3588029e..ef5cb0020 100644
--- a/kornia/feature/lightglue.py
+++ b/kornia/feature/lightglue.py
@@ -637,7 +637,6 @@ class LightGlue(nn.Module):
pattern = f"cross_attn.{i}", f"transformers.{i}.cross_attn"
state_dict = {k.replace(*pattern): v for k, v in state_dict.items()}
self.load_state_dict(state_dict, strict=False)
- print("Loaded LightGlue model")
# static lengths LightGlue is compiled for (only used with torch.compile)
self.static_lengths = None
diff --git a/kornia/geometry/transform/image_registrator.py b/kornia/geometry/transform/image_registrator.py
index 7d24770a0..ce04078a6 100644
--- a/kornia/geometry/transform/image_registrator.py
+++ b/kornia/geometry/transform/image_registrator.py
@@ -358,7 +358,7 @@ class ImageRegistrator(nn.Module):
prev_loss = current_loss
loss.backward()
if verbose and (i % 10 == 0):
- print(f"Loss = {current_loss:.4f}, iter={i}")
+ print(f"Loss = {current_loss:.4f}, iter={i}") # noqa: T201 - only with verbose=True
opt.step()
if output_intermediate_models:
aux_models.append(self.model().clone().detach())
diff --git a/kornia/image/image.py b/kornia/image/image.py
index 3b760b82e..8e96a2916 100644
--- a/kornia/image/image.py
+++ b/kornia/image/image.py
@@ -399,4 +399,4 @@ class Image:
data = self.data
if self.channels_order == ChannelsOrder.CHANNELS_LAST:
data = data.permute(2, 0, 1)
- print(image_to_string(data, max_width))
+ print(image_to_string(data, max_width)) # noqa: T201 - Image.print prints by design
diff --git a/kornia/image/image_print.py b/kornia/image/image_print.py
index 916e4dc0e..3fc52927a 100644
--- a/kornia/image/image_print.py
+++ b/kornia/image/image_print.py
@@ -421,4 +421,4 @@ def print_image(image: Union[str, torch.Tensor], max_width: int = 96) -> None:
img = image
else:
raise RuntimeError(f"Expect image type to be either torch.Tensor or str. Got {type(image)}.")
- print(image_to_string(img, max_width))
+ print(image_to_string(img, max_width)) # noqa: T201 - print_image prints by design
diff --git a/pyproject.toml b/pyproject.toml
index ee97ee8a7..dab98e377 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -243,6 +243,7 @@ select = [
"SIM910", # dict-get-with-none-default
"SLOT", # flake8-slots
"T10", # flake8-debugger
+ "T201", # flake8-print: print in the library; see the per-file ignore below
"TID", # flake8-tidy-imports
"UP", # pyupgrade
"W", # pycodestyle
@@ -275,7 +276,7 @@ select = [
# "Q", # flake8-quotes. No: ruff format handles quotes
# "SIM", # flake8-simplify. Partial: the SIM rules above. No: SIM300, it reads names such as `H` as constants
# "SLF", # flake8-self. No: tests check private members on purpose
- # "T20", # flake8-print. Partial: T201 is wanted for `kornia/` only
+ # "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)
@@ -320,6 +321,7 @@ max-returns = 13 # Recommended: 6
max-statements = 64 # Recommended: 50
[tool.ruff.lint.per-file-ignores]
+"!kornia/**" = ["T201"] # T201 applies to the library only
"*/__init__.py" = ["F401", "F403"] # Allow unused imports and star imports
"benchmarks/*" = [
"BLE",