From ef404cff8e1936b2f218477955a56ceb99642e70 Mon Sep 17 00:00:00 2001 From: Peter Corke Date: Mon, 23 Nov 2026 08:10:14 +1000 Subject: [PATCH] feat: make sum() work on feature sets and raise clear TypeErrors 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 --- .../ImagePointFeatures.py | 43 ++++++++++++++----- tests/test_image_point_features_extra.py | 37 +++++++++++++++- 2 files changed, 68 insertions(+), 12 deletions(-) diff --git a/src/machinevisiontoolbox/ImagePointFeatures.py b/src/machinevisiontoolbox/ImagePointFeatures.py index 323404e2..3654d15b 100644 --- a/src/machinevisiontoolbox/ImagePointFeatures.py +++ b/src/machinevisiontoolbox/ImagePointFeatures.py @@ -314,7 +314,8 @@ def __add__(self, other: "BaseFeature2D | builtins.list | None") -> "BaseFeature :param other: set of features :type other: :class:`BaseFeature2D` - :raises TypeError: _description_ + :raises TypeError: ``other`` is not a feature set, ``[]`` or ``None``, or + the two sets are of different feature types :return: set of features :rtype: :class:`BaseFeature2D` instance @@ -322,6 +323,10 @@ def __add__(self, other: "BaseFeature2D | builtins.list | None") -> "BaseFeature equal to ``None`` or ``[]`` it is interpreted as an empty feature set, this is useful in a loop for aggregating feature sets. + .. versionchanged:: 2.5.0 + Adding something that is not a feature set, ``[]`` or ``None`` + raises ``TypeError``. It used to raise ``AssertionError``. + Example: .. runblock:: pycon @@ -342,9 +347,11 @@ def __add__(self, other: "BaseFeature2D | builtins.list | None") -> "BaseFeature isinstance(other, list) and len(other) == 0 ): # Add feature sets return self - assert isinstance( - other, BaseFeature2D - ), f"cannot add {type(other)} to BaseFeature2D" + if not isinstance(other, BaseFeature2D): + raise TypeError( + f"cannot add {type(other).__name__} to a feature set, " + "only another feature set, [] or None" + ) if self._feature_type != other._feature_type: raise TypeError( "can't add different feature types:", @@ -364,13 +371,20 @@ def __radd__(self, other) -> "BaseFeature2D": :param other: set of features :type other: :class:`BaseFeature2D` - :raises TypeError: _description_ + :raises TypeError: ``other`` is not a feature set, ``[]``, ``None`` or ``0`` :return: set of features :rtype: :class:`BaseFeature2D` Add two feature sets to form a new feature sets. If ``other`` is - equal to ``None`` or ``[]`` it is interpreted as an empty feature - set, this is useful in a loop for aggregating feature sets. + equal to ``None``, ``[]`` or ``0`` it is interpreted as an empty feature + set, this is useful in a loop for aggregating feature sets. The ``0`` + is the starting value of the builtin :func:`sum`, so a list of + feature sets can be combined by ``sum(features)``. + + .. versionchanged:: 2.5.0 + ``None`` and ``0`` are accepted, and anything else raises + ``TypeError`` with a message. It used to raise ``ValueError("bad")``, + and ``sum()`` of feature sets did not work. Example: @@ -385,13 +399,22 @@ def __radd__(self, other) -> "BaseFeature2D": >>> orb += img1.ORB() >>> orb += img2.ORB() >>> orb + >>> orb = sum([img1.ORB(), img2.ORB()]) + >>> orb :seealso: :meth:`__add__` """ - if isinstance(other, list) and len(other) == 0: # Add feature sets + # an empty set, or the 0 that sum() starts with + if ( + other is None + or (isinstance(other, list) and len(other) == 0) + or (isinstance(other, int) and not isinstance(other, bool) and other == 0) + ): return self - else: - raise ValueError("bad") + raise TypeError( + f"cannot add a feature set to {type(other).__name__}, " + "only to [], None or 0 (as the start of sum)" + ) @property @scalar_result diff --git a/tests/test_image_point_features_extra.py b/tests/test_image_point_features_extra.py index e862cca2..a05496ef 100644 --- a/tests/test_image_point_features_extra.py +++ b/tests/test_image_point_features_extra.py @@ -200,10 +200,43 @@ def test_different_types_are_an_error(self): a + b def test_adding_other_things_is_an_error(self): - with pytest.raises(AssertionError): + with pytest.raises(TypeError, match="cannot add int to a feature set"): make_features(3) + 5 - with pytest.raises(ValueError): + with pytest.raises(TypeError, match="cannot add a feature set to int"): 5 + make_features(3) + with pytest.raises(TypeError, match="cannot add a feature set to str"): + "a" + make_features(3) + with pytest.raises(TypeError): + [1] + make_features(3) + with pytest.raises(TypeError): + True + make_features(3) + + def test_none_and_zero_on_the_left(self): + a = make_features(3) + # None cannot be on the left of + in Python, so call the method + assert a.__radd__(None) is a + assert (0 + a) is a + + def test_sum_of_feature_sets(self): + a, b, c = make_features(3), make_features(2), make_features(4) + total = sum([a, b, c]) + assert len(total) == 9 + assert total.descriptor.shape == (9, 4) + # the inputs are not changed + assert len(a) == 3 and len(b) == 2 and len(c) == 4 + nt.assert_array_equal(total.u[:3], a.u) + nt.assert_array_equal(total.u[3:5], b.u) + + def test_sum_of_one_and_of_a_generator(self): + a = make_features(3) + assert sum([a]) is a + assert len(sum(make_features(n) for n in (1, 2, 3))) == 6 + + def test_sum_of_different_types_is_an_error(self): + a, b = make_features(3), make_features(3) + b._feature_type = "ORB" + with pytest.raises(TypeError, match="different feature types"): + sum([a, b]) class TestGridify: