Skip to content

fix: replace 6 bare except: clauses with specific exception types - #31

Merged
petercorke merged 3 commits into
mainfrom
fix/bare-except-clauses
Jul 31, 2026
Merged

fix: replace 6 bare except: clauses with specific exception types#31
petercorke merged 3 commits into
mainfrom
fix/bare-except-clauses

Conversation

@petercorke

Copy link
Copy Markdown
Owner

Summary

Bare except: swallows everything including KeyboardInterrupt/SystemExit and hides the real failure mode. Narrowed all 6 in the codebase (confirmed via fresh grep — no more remain) to what the guarded code can actually raise:

Location Guards Narrowed to
BundleAdjust.py:15 import pgraph fallback ImportError
ImagePointFeatures.py:283 nw, nh = nbins unpack (scalar nbins is a valid fallback path) TypeError, ValueError
Camera.py:830 artist.remove() in clf(), arbitrary matplotlib Artist Exception
Camera.py:1865 arithmetic on possibly-unset imagesize/rho in fov() TypeError, AttributeError
ImageProcessing.py:1149 argcheck.getvector(image2, 3) Exception
__init__.py:191 importlib.metadata.version() lookup importlib.metadata.PackageNotFoundError

Also logs a real bug found while verifying this (gridify()'s nw, nh = nbins path) to tech-debt.md — since fixed and merged separately in #30, not bundled here.

Test plan

  • grep -rn "except:" src/machinevisiontoolbox/*.py — zero matches
  • Manually exercised the narrowed paths: fov() called before imagesize/rho set still raises the intended ValueError; choose() with an invalid image2 still raises the intended ValueError
  • Full suite: 816 passed, 15 skipped, no regressions

Confirmed pre-existing (git stash reproduces identically on unmodified
main) and unrelated to the except-type narrowing in this branch --
found only because verifying the narrowed exception type required
actually exercising gridify().
Bare except: swallows everything including KeyboardInterrupt and
SystemExit, and hides the real failure mode from readers. Narrowed
each to what the guarded code can actually raise:

- BundleAdjust.py:15 -- `import pgraph` fallback -> ImportError
- ImagePointFeatures.py:283 -- `nw, nh = nbins` unpack (scalar nbins
  is a valid, intentional fallback path, not an error) -> TypeError,
  ValueError
- Camera.py:830 -- `artist.remove()` in clf(), arbitrary matplotlib
  Artist -> Exception (can't narrow further without knowing every
  Artist subclass's failure mode)
- Camera.py:1865 -- arithmetic on possibly-unset imagesize/rho in
  fov() -> TypeError, AttributeError
- ImageProcessing.py:1149 -- argcheck.getvector(image2, 3) -> Exception
- __init__.py:191 -- importlib.metadata.version() lookup ->
  importlib.metadata.PackageNotFoundError

Verified each guarded path still gets caught correctly: fov() called
before imagesize/rho set still raises the intended ValueError;
choose() with an invalid image2 still raises the intended ValueError.
Full suite: 816 passed, 15 skipped, no regressions.
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 0 complexity · 0 duplication

Metric Results
Complexity 0
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@petercorke
petercorke merged commit 92dd050 into main Jul 31, 2026
30 of 32 checks passed
@petercorke
petercorke deleted the fix/bare-except-clauses branch July 31, 2026 08:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant