10000 FIX support both instance and class level methods in `_AvailableIfDescriptor.__get__` by harupy · Pull Request #20623 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

FIX support both instance and class level methods in _AvailableIfDescriptor.__get__ #20623

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
Jul 28, 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
8 changes: 6 additions & 2 deletions sklearn/utils/metaestimators.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,12 @@ def __get__(self, obj, owner=None):
f" {repr(self.attribute_name)}"
)

# lambda, but not partial, allows help() to work with update_wrapper
out = lambda *args, **kwargs: self.fn(obj, *args, **kwargs) # noqa
# lambda, but not partial, allows help() to work with update_wrapper
out = lambda *args, **kwargs: self.fn(obj, *args, **kwargs) # noqa
else:
# This makes it possible to use the decorated method as an unbound method,
# for instance when monkeypatching.
out = lambda *args, **kwargs: self.fn(*args, **kwargs) # noqa
# update the docstring of the returned function
update_wrapper(out, self.fn)
return out
Expand Down
7 changes: 7 additions & 0 deletions sklearn/utils/tests/test_metaestimators.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,10 @@ def test_available_if_docstring():
def test_available_if():
assert hasattr(AvailableParameterEstimator(), "available_func")
assert not hasattr(AvailableParameterEstimator(available=False), "available_func")

# This is a non regression test for:
# https://github.com/scikit-learn/scikit-learn/issues/20614
# to make sure that decorated functions can be used as an unbound method,
# for instance when monkeypatching.
est = AvailableParameterEstimator()
AvailableParameterEstimator.available_func(est)
0