Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(geometry): stop kornia.geometry leaking stdlib imports as public API (#4738)

Dipak Chaudhari6fdcc4c45a2d
diff --git a/changelog.d/4738.fixed.md b/changelog.d/4738.fixed.md
new file mode 100644
index 000000000..5da4ada03
--- /dev/null
+++ b/changelog.d/4738.fixed.md
@@ -0,0 +1 @@
+`kornia.geometry` no longer re-exports `sys`, `struct`, `array`, `os`, `BinaryIO`, `Dict`, `List`, `NamedTuple` and `is_compiling` from its `pointcloud` and `grid` submodules, which now declare `__all__`.
diff --git a/kornia/geometry/grid.py b/kornia/geometry/grid.py
index 0baf4eff2..35059abfb 100644
--- a/kornia/geometry/grid.py
+++ b/kornia/geometry/grid.py
@@ -22,6 +22,8 @@ import torch
from kornia.core.utils import is_compiling
+__all__ = ["create_meshgrid", "create_meshgrid3d"]
+
def create_meshgrid(
height: int,
diff --git a/kornia/geometry/pointcloud.py b/kornia/geometry/pointcloud.py
index 2143f25e1..27c097084 100644
--- a/kornia/geometry/pointcloud.py
+++ b/kornia/geometry/pointcloud.py
@@ -24,6 +24,13 @@ from typing import BinaryIO, Dict, List, NamedTuple, Optional, Tuple
import torch
+__all__ = [
+ "load_pointcloud_ply",
+ "load_pointcloud_ply_binary",
+ "save_pointcloud_ply",
+ "save_pointcloud_ply_binary",
+]
+
def save_pointcloud_ply(filename: str, pointcloud: torch.Tensor) -> None:
r"""Save to disk a pointcloud in PLY format.
diff --git a/tests/api_surface.json b/tests/api_surface.json
index 84f029b1b..ffc3b14f7 100644
--- a/tests/api_surface.json
+++ b/tests/api_surface.json
@@ -180,6 +180,7 @@
"ONNXMixin",
"ONNXRuntimeMixin",
"TensorWrapper",
+ "check_safetensors",
"download_file_from_url",
"download_hf_file",
"external",
@@ -491,6 +492,7 @@
"line",
"line_segment_transfer_error_one_way",
"load_pointcloud_ply",
+ "load_pointcloud_ply_binary",
"matrix4x4_to_Rt",
"motion_from_essential",
"motion_from_essential_choose_solution",
@@ -512,7 +514,6 @@
"normalize_transformation",
"null_vector_3x4",
"oneway_transfer_error",
- "os",
"pixel2cam",
"plane",
"point_line_distance",
@@ -522,6 +523,7 @@
"project_points",
"project_points_orthographic",
"project_points_z1",
+ "project_to_essential",
"projection_from_KRt",
"projection_from_Rt",
"projections_from_fundamental",
@@ -538,6 +540,7 @@
"rad2deg",
"random_intrinsics",
"ransac",
+ "ray",
"relative_camera_motion",
"relative_transformation",
"remap",
@@ -557,6 +560,7 @@
"sample_is_valid_for_homography",
"sampson_epipolar_distance",
"save_pointcloud_ply",
+ "save_pointcloud_ply_binary",
"scale",
"scale_intrinsics",
"shear",
@@ -690,17 +694,23 @@
"SSIM3D",
"accuracy",
"aepe",
+ "angle_error_mat",
+ "angle_error_vec",
+ "auc_from_errors",
"average_endpoint_error",
"confusion_matrix",
+ "kitti_d1_error",
"mean_absolute_disparity_error",
"mean_average_precision",
"mean_bad_pixel_error",
"mean_iou",
"mean_iou_bbox",
+ "pose_errors",
"psnr",
"root_mean_squared_disparity_error",
"ssim",
- "ssim3d"
+ "ssim3d",
+ "translation_ate"
],
"kornia.morphology": [
"bottom_hat",
diff --git a/tests/test_api_surface.py b/tests/test_api_surface.py
index 2cbbe6bff..e281f6814 100644
--- a/tests/test_api_surface.py
+++ b/tests/test_api_surface.py
@@ -34,6 +34,7 @@ import contextlib
import importlib
import json
import pkgutil
+import types
import warnings
from pathlib import Path
@@ -89,6 +90,53 @@ def test_no_public_name_removed(module_name):
)
+def _foreign_names(module_name: str) -> set:
+ """Public names of ``module_name`` bound to an import rather than to its own API.
+
+ That is another package's module, a ``typing`` object, or an object defined in a kornia
+ module outside ``module_name``'s own subtree. A module without ``__all__`` exposes whatever
+ its star-imported submodules happen to import, so ``import sys`` in one of them makes
+ ``kornia.geometry.sys`` public, and ``from kornia.core.utils import is_compiling`` makes
+ ``kornia.geometry.is_compiling`` public (#4709).
+ """
+ mod = importlib.import_module(module_name)
+ found = set()
+ for name in _current_surface(module_name):
+ obj = getattr(mod, name, None)
+ if isinstance(obj, types.ModuleType):
+ if obj.__name__ != "kornia" and not obj.__name__.startswith("kornia."):
+ found.add(name)
+ else:
+ owner = getattr(obj, "__module__", None)
+ if owner == "typing":
+ found.add(name)
+ elif (
+ isinstance(owner, str)
+ and owner.startswith("kornia.")
+ and owner != module_name
+ and not owner.startswith(module_name + ".")
+ ):
+ found.add(name)
+ return found
+
+
[email protected]("module_name", sorted(json.loads(INVENTORY.read_text())))
+def test_no_foreign_name_leaks_into_the_surface(module_name):
+ """An import inside a submodule must not become public API by accident.
+
+ Additions do not fail ``test_no_public_name_removed``, and ``regenerate()`` would
+ record a leaked ``sys`` as a guarded public name. Names already in the inventory are
+ exempt: removing one is a deliberate API change, not something this test should force.
+ """
+ recorded = set(json.loads(INVENTORY.read_text())[module_name])
+ leaked = sorted(_foreign_names(module_name) - recorded)
+ assert not leaked, (
+ f"{module_name} exposes {leaked}, which are imports rather than kornia API. Give the "
+ "submodule that imports them an __all__ (or import them under a private name). Record a "
+ "name in tests/api_surface.json only for a deliberate cross-package re-export."
+ )
+
+
def _modules_declaring_all() -> list:
"""Every importable ``kornia`` submodule that declares ``__all__``.