diff --git a/sklearn/utils/deprecation.py b/sklearn/utils/deprecation.py index 5621f436d9baf..fc06f9bc84d3b 100644 --- a/sklearn/utils/deprecation.py +++ b/sklearn/utils/deprecation.py @@ -78,6 +78,9 @@ def wrapped(*args, **kwargs): return fun(*args, **kwargs) wrapped.__doc__ = self._update_doc(wrapped.__doc__) + # Add a reference to the wrapped function so that we can introspect + # on function arguments in Python 2 (already works in Python 3) + wrapped.__wrapped__ = fun return wrapped diff --git a/sklearn/utils/tests/test_estimator_checks.py b/sklearn/utils/tests/test_estimator_checks.py index 53a67693843d9..049dff4baa920 100644 --- a/sklearn/utils/tests/test_estimator_checks.py +++ b/sklearn/utils/tests/test_estimator_checks.py @@ -9,12 +9,14 @@ from sklearn.externals import joblib from sklearn.base import BaseEstimator, ClassifierMixin +from sklearn.utils import deprecated from sklearn.utils.testing import (assert_raises_regex, assert_true, assert_equal, ignore_warnings) from sklearn.utils.estimator_checks import check_estimator from sklearn.utils.estimator_checks import set_random_state from sklearn.utils.estimator_checks import set_checking_parameters from sklearn.utils.estimator_checks import check_estimators_unfitted +from sklearn.utils.estimator_checks import check_fit_score_takes_y from sklearn.utils.estimator_checks import check_no_attributes_set_in_init from sklearn.ensemble import AdaBoostClassifier, RandomForestClassifier from sklearn.linear_model import LinearRegression, SGDClassifier @@ -176,6 +178,19 @@ def transform(self, X): return sp.csr_matrix(X) +def test_check_fit_score_takes_y_works_on_deprecated_fit(): + # Tests that check_fit_score_takes_y works on a class with + # a deprecated fit method + + class TestEstimatorWithDeprecatedFitMethod(BaseEstimator): + @deprecated("Deprecated for the purpose of testing " + "check_fit_score_takes_y") + def fit(self, X, y): + return self + + check_fit_score_takes_y("test", TestEstimatorWithDeprecatedFitMethod()) + + def test_check_estimator(): # tests that the estimator actually fails on "bad" estimators. # not a complete test of all checks, which are very extensive. diff --git a/sklearn/utils/tests/test_validation.py b/sklearn/utils/tests/test_validation.py index 076e6d88440f1..5af26ac5b978f 100644 --- a/sklearn/utils/tests/test_validation.py +++ b/sklearn/utils/tests/test_validation.py @@ -22,6 +22,7 @@ from sklearn.utils.testing import assert_allclose_dense_sparse from sklearn.utils import as_float_array, check_array, check_symmetric from sklearn.utils import check_X_y +from sklearn.utils import deprecated from sklearn.utils.mocking import MockDataFrame from sklearn.utils.estimator_checks import NotAnArray from sklearn.random_projection import sparse_random_matrix @@ -563,6 +564,15 @@ def test_has_fit_parameter(): assert_true(has_fit_parameter(SVR, "sample_weight")) assert_true(has_fit_parameter(SVR(), "sample_weight")) + class TestClassWithDeprecatedFitMethod: + @deprecated("Deprecated for the purpose of testing has_fit_parameter") + def fit(self, X, y, sample_weight=None): + pass + + assert has_fit_parameter(TestClassWithDeprecatedFitMethod, + "sample_weight"), \ + "has_fit_parameter fails for class with deprecated fit method." + def test_check_symmetric(): arr_sym = np.array([[0, 1], [1, 2]])