From b82415c118646140e6af2c21a861da02a447c040 Mon Sep 17 00:00:00 2001 From: Peter Corke Date: Sun, 22 Nov 2026 21:02:20 +1000 Subject: [PATCH] fix: return None from the descriptor property when there are none The descriptor property is typed ndarray | None, but its array_result decorator called len() on the value, so it raised TypeError for features with no descriptors. It now returns None, and keeps the single-feature behaviour of giving a 1D vector. With the property honestly typed, match() checks both sets have descriptors and raises a clear ValueError, as distance() already does, and descriptor1 and descriptor2 of a FeatureMatch are typed as possibly None. The xfail test that recorded the bug is now an ordinary test, and a second test covers match(). Co-Authored-By: Claude Sonnet 5.5 --- .../ImagePointFeatures.py | 29 ++++++++++++++----- tests/test_image_point_features_extra.py | 14 +++++---- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/machinevisiontoolbox/ImagePointFeatures.py b/src/machinevisiontoolbox/ImagePointFeatures.py index b50c3dd0..18c2ff02 100644 --- a/src/machinevisiontoolbox/ImagePointFeatures.py +++ b/src/machinevisiontoolbox/ImagePointFeatures.py @@ -546,13 +546,12 @@ def octave(self) -> "builtins.list[int]": # Octave of feature return [kp.octave for kp in self._kp] @property - @array_result def descriptor(self) -> np.ndarray | None: # Descriptor of feature """ Descriptor of feature - :return: Descriptor - :rtype: ndarray(N,M) + :return: Descriptor, or None if the features have no descriptors + :rtype: ndarray(N,M) or None Example: @@ -566,8 +565,17 @@ def descriptor(self) -> np.ndarray | None: # Descriptor of feature >>> orb[:5].descriptor.shape .. note:: For single feature return a 1D array vector, for multiple features return a set of column vectors. + + .. versionchanged:: 2.5.0 + Returns None, instead of raising ``TypeError``, if the features have + no descriptors. """ - return self._descriptor + des = self._descriptor + if des is None: + return None + # a single feature gives a 1D vector, as array_result does for the + # other properties + return des[0] if len(des) == 1 else des @property @array_result @@ -731,8 +739,13 @@ def match( # sorting # return + des1 = self.descriptor + des2 = other.descriptor + if des1 is None or des2 is None: + raise ValueError("features have no descriptors") + if metric is None: - metric = "hamming" if self.descriptor.dtype == np.uint8 else "L2" + metric = "hamming" if des1.dtype == np.uint8 else "L2" metricdict = { "L1": cv2.NORM_L1, @@ -755,7 +768,7 @@ def match( else: k = 2 - matches0 = bf.knnMatch(self.descriptor, other.descriptor, k=k) + matches0 = bf.knnMatch(des1, des2, k=k) # the elements of matches are: # queryIdx: first feature set (self) @@ -1834,7 +1847,7 @@ def p2(self) -> "builtins.list[np.ndarray]": @property @array_result - def descriptor1(self) -> "builtins.list[np.ndarray]": + def descriptor1(self) -> "builtins.list[np.ndarray | None]": """ Feature descriptor in first image @@ -1856,7 +1869,7 @@ def descriptor1(self) -> "builtins.list[np.ndarray]": @property @array_result - def descriptor2(self) -> "builtins.list[np.ndarray]": + def descriptor2(self) -> "builtins.list[np.ndarray | None]": """ Feature descriptor in second image diff --git a/tests/test_image_point_features_extra.py b/tests/test_image_point_features_extra.py index 50f16a71..0a3107ea 100644 --- a/tests/test_image_point_features_extra.py +++ b/tests/test_image_point_features_extra.py @@ -99,12 +99,6 @@ def test_without_descriptors(self): assert len(f) == 2 assert f._descriptor is None - @pytest.mark.xfail( - strict=True, - raises=TypeError, - reason="descriptor is typed as ndarray | None but array_result calls " - "len() on it, so it raises when there are no descriptors", - ) def test_descriptor_property_without_descriptors(self): assert make_features(descriptors=False).descriptor is None @@ -340,6 +334,14 @@ def test_different_sizes(self): assert D.shape == (3, 4) +class TestMatchNeedsDescriptors: + def test_no_descriptors_is_a_clear_error(self): + with pytest.raises(ValueError, match="no descriptors"): + make_features(descriptors=False).match(make_features()) + with pytest.raises(ValueError, match="no descriptors"): + make_features().match(make_features(descriptors=False)) + + class TestMatch: @pytest.fixture def pair(self):