Skip to content

chore: make error messages say what was received and what is allowed - #244

Merged
petercorke merged 2 commits into
mainfrom
chore/clearer-error-messages
Nov 23, 2026
Merged

petercorke merged 2 commits into
mainfrom
chore/clearer-error-messages

Conversation

@petercorke

Copy link
Copy Markdown
Owner

Summary

A survey of the 402 raise statements in src found 63 whose messages were missing or unhelpful. This rewrites them. No exception type changes, and no behaviour changes except the text.

  • 43 passed the value and the text as two arguments, for example ValueError(n, "n must be greater than 0"). Python prints that as the tuple (3, 'n must be greater than 0'). The value is now in an f-string: n must be greater than 0, got 3. The original wording is kept as the start of each message, so anything matching on it still works. A few sites got a better message where the old one was misleading or lacked information: ImageSpatial.convolve said opt is not a valid option for the mode argument, the border and mode errors now list the allowed values, the pattern-falls-off-edge errors in ImageProcessing give the position and sizes, ImageWholeFeatures moment checks show both p and q, and Image must be greyscale in ImageColor gives the number of planes.
  • 19 had no message or one of two words, such as bad type (Camera.F and Camera.E), bad index, bad id, unknown detector, unknown style, unknown type, unknown colorspace and bad colorspace (four in color.py), bad input (draw_point), bad s (mkgrid), planes mismatch, invalid slice, solvePnP failed and truncated (two in Sources.py). Each now names the argument, what is allowed and what was received.
  • VisualServo.step raised NotImplementedError with no message, and now names the class that must implement it.

The 13 raise StopIteration statements in Sources.py are correct for the iterator protocol and are left alone.

Tests

tests/test_error_messages.py is new, with checks that messages name the value for a sample of the changes. Three existing tests that matched the old text (bad index, bad id and the border message) are updated. The full suite passes locally (2104 passed, 33 skipped) and mypy reports no errors.

Not changed

While doing this I found two unrelated faults, which are not touched here: name2color catches ValueError around the whole conversion, so an unknown colorspace returns None and its new message is never seen, and plot_spectral_locus tests colorspace in ("rg"), which is a substring test, not a tuple.

This change was written with AI assistance (Claude Code) and I have reviewed the diff.

🤖 Generated with Claude Code

Rewrite 63 raise statements whose messages were missing or unhelpful:

- 43 passed the value and the text as two arguments, so the message
  printed as a tuple, for example (3, 'n must be greater than 0'). The
  value is now in the text: "n must be greater than 0, got 3".
- 19 had no message or one of two words, such as "bad type", "bad id",
  "bad colorspace" and "truncated". They now name the argument, the
  allowed values and what was received.
- the NotImplementedError in VisualServo.step names the class that must
  implement it.

No exception types change.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 7 complexity · 0 duplication

Metric Results
Complexity 7
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 afc813b into main Nov 23, 2026
61 of 63 checks passed
@petercorke
petercorke deleted the chore/clearer-error-messages branch November 23, 2026 04:22
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