Skip to content

feat: make sum() work on feature sets and raise clear TypeErrors - #243

Merged
petercorke merged 2 commits into
mainfrom
bug/features-sum
Nov 22, 2026
Merged

petercorke merged 2 commits into
mainfrom
bug/features-sum

Conversation

@petercorke

Copy link
Copy Markdown
Owner

Summary

sum() of feature sets did not work. Python's sum starts from 0, so sum([f1, f2]) calls f1.__radd__(0), and BaseFeature2D.__radd__ accepted only [] and raised ValueError("bad") for anything else. Aggregating feature sets with [] and += still works as before.

  • __radd__ now also accepts None and 0, so sum(list_of_feature_sets) works, for lists and generators. The inputs are not changed, and sum([f]) returns f.
  • __radd__ raises TypeError with a message that names the type and says what can be added ([], None or 0), instead of ValueError("bad").
  • __add__ raises TypeError with a message for something that is not a feature set, [] or None, instead of failing an assert. This changes the exception from AssertionError to TypeError, which is what its docstring already said.
  • Docstrings are updated, the __radd__ example shows sum, and there are .. versionchanged:: 2.5.0 notes.

Tests

New tests: sum of three feature sets (count, descriptors, order, inputs unchanged), sum of one set and of a generator, sum of different feature types raising TypeError, 0 and None on the left, and the TypeError messages for int, str, bool and list operands. tests/test_image_point_features_extra.py passes (127 tests) and mypy reports no errors. I have not run the full suite locally. CI will.

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

🤖 Generated with Claude Code

BaseFeature2D.__radd__ accepted only [], so sum() of feature sets, which
starts from 0, raised ValueError("bad"). It now also accepts None and 0,
and raises TypeError with a message for anything else. __add__ raises
TypeError instead of failing an assert for something that is not a
feature set.

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

Copy link
Copy Markdown

Not up to standards ⛔

🔴 Issues 3 high

Alerts:
⚠ 3 issues (≤ 0 issues of at least minor severity)

Results:
3 new issues

Category Results
ErrorProne 3 high

View in Codacy

🟢 Metrics 24 complexity · 0 duplication

Metric Results
Complexity 24
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 cb30d19 into main Nov 22, 2026
33 of 34 checks passed
@petercorke
petercorke deleted the bug/features-sum branch November 22, 2026 23:31
@github-actions github-actions Bot mentioned this pull request Nov 22, 2026
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