Skip to content

FIX avoid scalar/array boolean operation in _IffHasAttrDescriptor #21145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/whats_new/v1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ Fixed models
`CVE-2020-28975 <https://nvd.nist.gov/vuln/detail/CVE-2020-28975>`__.
:pr:`21336` by `Thomas Fan`_.

:mod:`sklearn.utils`
....................

- |Fix| Solve a bug in :func:`~sklearn.utils.metaestimators.if_delegate_has_method`
where the underlying check for an attribute did not work with NumPy arrays.
:pr:`21145` by :user:`Zahlii <Zahlii>`.

.. _changes_1_0:

Version 1.0.0
Expand Down
4 changes: 3 additions & 1 deletion sklearn/utils/metaestimators.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,9 @@ def _check(self, obj):
if delegate is None:
return False
# raise original AttributeError
return getattr(delegate, self.attribute_name) or True
getattr(delegate, self.attribute_name)

return True


def if_delegate_has_method(delegate):
Expand Down
17 changes: 17 additions & 0 deletions sklearn/utils/tests/test_metaestimators.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import numpy as np
import pytest

from sklearn.utils.metaestimators import if_delegate_has_method
Expand Down Expand Up @@ -69,6 +70,12 @@ class HasNoPredict:
pass


class HasPredictAsNDArray:
"""A mock sub-estimator where predict is a NumPy array"""

predict = np.ones((10, 2), dtype=np.int64)


def test_if_delegate_has_method():
assert hasattr(MetaEst(HasPredict()), "predict")
assert not hasattr(MetaEst(HasNoPredict()), "predict")
Expand Down Expand Up @@ -122,3 +129,13 @@ def test_available_if_unbound_method():
match="This 'AvailableParameterEstimator' has no attribute 'available_func'",
):
AvailableParameterEstimator.available_func(est)


def test_if_delegate_has_method_numpy_array():
"""Check that we can check for an attribute that is a NumPy array.

This is a non-regression test for:
https://github.com/scikit-learn/scikit-learn/issues/21144
"""
estimator = MetaEst(HasPredictAsNDArray())
assert hasattr(estimator, "predict")