8000 FIX support both instance and class level methods in `_AvailableIfDes… · rth/scikit-learn@f2f8b52 · GitHub
[go: up one dir, main page]

Skip to content

Commit f2f8b52

Browse files
authored
FIX support both instance and class level methods in _AvailableIfDescriptor.__get__ (scikit-learn#20623)
1 parent 2c2c6ca commit f2f8b52

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

sklearn/utils/metaestimators.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,12 @@ def __get__(self, obj, owner=None):
110110
f" {repr(self.attribute_name)}"
111111
)
112112

113-
# lambda, but not partial, allows help() to work with update_wrapper
114-
out = lambda *args, **kwargs: self.fn(obj, *args, **kwargs) # noqa
113+
# lambda, but not partial, allows help() to work with update_wrapper
114+
out = lambda *args, **kwargs: self.fn(obj, *args, **kwargs) # noqa
115+
else:
116+
# This makes it possible to use the decorated method as an unbound method,
117+
# for instance when monkeypatching.
118+
out = lambda *args, **kwargs: self.fn(*args, **kwargs) # noqa
115119
# update the docstring of the returned function
116120
update_wrapper(out, self.fn)
117121
return out

sklearn/utils/tests/test_metaestimators.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,10 @@ def test_available_if_docstring():
104104
def test_available_if():
105105
assert hasattr(AvailableParameterEstimator(), "available_func")
106106
assert not hasattr(AvailableParameterEstimator(available=False), "available_func")
107+
108+
# This is a non regression test for:
109+
# https://github.com/scikit-learn/scikit-learn/issues/20614
110+
# to make sure that decorated functions can be used as an unbound method,
111+
# for instance when monkeypatching.
112+
est = AvailableParameterEstimator()
113+
AvailableParameterEstimator.available_func(est)

0 commit comments

Comments
 (0)
0