Skip to content

Commit 8a09fd5

Browse files
petercorkeclaude
andcommitted
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 <noreply@anthropic.com>
1 parent 3636461 commit 8a09fd5

3 files changed

Lines changed: 11 additions & 6 deletions

File tree

‎src/machinevisiontoolbox/BundleAdjust.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,8 @@ def add_projection(
405405
406406
:seealso: :meth:`add_view` :meth:`add_landmark`
407407
"""
408-
assert len(uv) == 2, "uv must be a 2-vector"
408+
if len(uv) != 2:
409+
raise ValueError("uv must be a 2-vector")
409410

410411
edge = Observation(viewpoint, landmark, uv.flatten()) # create edge object
411412
e = viewpoint.connect(landmark, edge=edge) # connect nodes with it

‎src/machinevisiontoolbox/Camera.py‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,8 @@ def nu(self) -> int:
350350
351351
:seealso: :meth:`nv` :meth:`width` :meth:`imagesize`
352352
"""
353-
assert self._imagesize is not None, "imagesize not set"
353+
if self._imagesize is None:
354+
raise ValueError("imagesize not set")
354355
return self._imagesize[0]
355356

356357
@property
@@ -370,7 +371,8 @@ def nv(self) -> int:
370371
371372
:seealso: :meth:`nu` :meth:`height` :meth:`imagesize`
372373
"""
373-
assert self._imagesize is not None, "imagesize not set"
374+
if self._imagesize is None:
375+
raise ValueError("imagesize not set")
374376
return self._imagesize[1]
375377

376378
@property
@@ -390,7 +392,8 @@ def width(self) -> int:
390392
391393
:seealso: :meth:`nu` :meth:`height`
392394
"""
393-
assert self._imagesize is not None, "imagesize not set"
395+
if self._imagesize is None:
396+
raise ValueError("imagesize not set")
394397
return self._imagesize[0]
395398

396399
@property
@@ -410,7 +413,8 @@ def height(self) -> int:
410413
411414
:seealso: :meth:`nv` :meth:`width`
412415
"""
413-
assert self._imagesize is not None, "imagesize not set"
416+
if self._imagesize is None:
417+
raise ValueError("imagesize not set")
414418
return self._imagesize[1]
415419

416420
@property

‎src/machinevisiontoolbox/Sources.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2348,7 +2348,7 @@ def _pointcloud_to_ros_message(
23482348
"Install it with: pip install open3d "
23492349
"or pip install machinevision-toolbox-python[open3d]"
23502350
)
2351-
assert o3d is not None
2351+
assert o3d is not None
23522352

23532353
points = np.asarray(pc._pcd.points)
23542354
if points.ndim != 2 or points.shape[1] != 3:

0 commit comments

Comments
 (0)