From 7afe33eb02af65d4e64d52481f668a610f63b807 Mon Sep 17 00:00:00 2001 From: Peter Corke Date: Sun, 16 Aug 2026 13:37:19 +1000 Subject: [PATCH] fix: assert hygiene -- unreachable assert, two caller-facing checks Found while surveying src/'s ~48 Bandit B101 (assert_used) findings; most are legitimate (type-narrowing after a prior check, or optional-dependency guards immediately following an ImportError raise) and are staying as-is. Three were real issues: - Sources.py (PointCloud publish support): `assert o3d is not None` sat *inside* the `if not _open3d_available: raise ImportError(...)` block, after the raise -- unreachable dead code, apparently a mis-indented copy of the same pattern used correctly at 7 other call sites in this file. Dedented to match. - BundleAdjust.py: `add_projection()`'s `assert len(uv) == 2` validates a caller-supplied argument on a public method, not an internal invariant -- converted to `if len(uv) != 2: raise ValueError(...)` so it survives `python -O` instead of silently vanishing. - Camera.py: `nu`/`nv`/`width`/`height` each asserted `self._imagesize is not None, "imagesize not set"` -- a real "you called this before configuring the camera" caller mistake (the informative message was the tell), not an internal invariant. Converted to `if self._imagesize is None: raise ValueError(...)`, matching the existing `raise ValueError("imagesize or rho properties not set")` convention already used elsewhere in this file. Co-Authored-By: Claude Sonnet 5 --- src/machinevisiontoolbox/BundleAdjust.py | 3 ++- src/machinevisiontoolbox/Camera.py | 12 ++++++++---- src/machinevisiontoolbox/Sources.py | 2 +- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/machinevisiontoolbox/BundleAdjust.py b/src/machinevisiontoolbox/BundleAdjust.py index 67b41f28..fae5d2ce 100644 --- a/src/machinevisiontoolbox/BundleAdjust.py +++ b/src/machinevisiontoolbox/BundleAdjust.py @@ -405,7 +405,8 @@ def add_projection( :seealso: :meth:`add_view` :meth:`add_landmark` """ - assert len(uv) == 2, "uv must be a 2-vector" + if len(uv) != 2: + raise ValueError("uv must be a 2-vector") edge = Observation(viewpoint, landmark, uv.flatten()) # create edge object e = viewpoint.connect(landmark, edge=edge) # connect nodes with it diff --git a/src/machinevisiontoolbox/Camera.py b/src/machinevisiontoolbox/Camera.py index 9be5c404..e7c76e98 100644 --- a/src/machinevisiontoolbox/Camera.py +++ b/src/machinevisiontoolbox/Camera.py @@ -350,7 +350,8 @@ def nu(self) -> int: :seealso: :meth:`nv` :meth:`width` :meth:`imagesize` """ - assert self._imagesize is not None, "imagesize not set" + if self._imagesize is None: + raise ValueError("imagesize not set") return self._imagesize[0] @property @@ -370,7 +371,8 @@ def nv(self) -> int: :seealso: :meth:`nu` :meth:`height` :meth:`imagesize` """ - assert self._imagesize is not None, "imagesize not set" + if self._imagesize is None: + raise ValueError("imagesize not set") return self._imagesize[1] @property @@ -390,7 +392,8 @@ def width(self) -> int: :seealso: :meth:`nu` :meth:`height` """ - assert self._imagesize is not None, "imagesize not set" + if self._imagesize is None: + raise ValueError("imagesize not set") return self._imagesize[0] @property @@ -410,7 +413,8 @@ def height(self) -> int: :seealso: :meth:`nv` :meth:`width` """ - assert self._imagesize is not None, "imagesize not set" + if self._imagesize is None: + raise ValueError("imagesize not set") return self._imagesize[1] @property diff --git a/src/machinevisiontoolbox/Sources.py b/src/machinevisiontoolbox/Sources.py index 5de72c67..7b05484f 100644 --- a/src/machinevisiontoolbox/Sources.py +++ b/src/machinevisiontoolbox/Sources.py @@ -2348,7 +2348,7 @@ def _pointcloud_to_ros_message( "Install it with: pip install open3d " "or pip install machinevision-toolbox-python[open3d]" ) - assert o3d is not None + assert o3d is not None points = np.asarray(pc._pcd.points) if points.ndim != 2 or points.shape[1] != 3: