8000 FIX avoid scalar/array boolean operation in _IffHasAttrDescriptor (#2… · scikit-learn/scikit-learn@896ad12 · GitHub
[go: up one dir, main page]

Skip to content

Commit 896ad12

Browse files
Zahliiglemaitre
authored andcommitted
FIX avoid scalar/array boolean operation in _IffHasAttrDescriptor (#21145)
1 parent e8ffe52 commit 896ad12

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

doc/whats_new/v1.0.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ Fixed models
7676
`CVE-2020-28975 <https://nvd.nist.gov/vuln/detail/CVE-2020-28975>`__.
7777
:pr:`21336` by `Thomas Fan`_.
7878

79+
:mod:`sklearn.utils`
80+
....................
81+
82+
- |Fix| Solve a bug in :func:`~sklearn.utils.metaestimators.if_delegate_has_method`
83+
where the underlying check for an attribute did not work with NumPy arrays.
84+
:pr:`21145` by :user:`Zahlii <Zahlii>`.
85+
7986
.. _changes_1_0:
8087

8188
Version 1.0.0

sklearn/utils/metaestimators.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,9 @@ def _check(self, obj):
194194
if delegate is None:
195195
return False
196196
# raise original AttributeError
197-
return getattr(delegate, self.attribute_name) or True
197+
getattr(delegate, self.attribute_name)
198+
199+
return True
198200

199201

200202
def if_delegate_has_method(delegate):

sklearn/utils/tests/test_metaestimators.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import numpy as np
12
import pytest
23

34
from sklearn.utils.metaestimators import if_delegate_has_method
@@ -69,6 +70,12 @@ class HasNoPredict:
6970
pass
7071

7172

73+
class HasPredictAsNDArray:
74+
"""A mock sub-estimator where predict is a NumPy array"""
75+
76+
predict = np.ones((10, 2), dtype=np.int64)
77+
78+
7279
def test_if_delegate_has_method():
7380
assert hasattr(MetaEst(HasPredict()), "predict")
7481
assert not hasattr(MetaEst(HasNoPredict()), "predict")
@@ -122,3 +129,13 @@ def test_available_if_unbound_method():
122129
match="This 'AvailableParameterEstimator' has no attribute 'available_func'",
123130
):
124131
AvailableParameterEstimator.available_func(est)
132+
133+
134+
def test_if_delegate_has_method_numpy_array():
135+
"""Check that we can check for an attribute that is a NumPy array.
136+
137+
This is a non-regression test for:
138+
https://github.com/scikit-learn/scikit-learn/issues/21144
139+
"""
140+
estimator = MetaEst(HasPredictAsNDArray())
141+
assert hasattr(estimator, "predict")

0 commit comments

Comments
 (0)
0