From 5007e02c88106969553ff453e7e5b66ffa98489f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Est=C3=A8ve?= Date: Tue, 13 Dec 2016 10:59:31 +0100 Subject: [PATCH] FIX .format arguments were in the wrong order Add check_no_fit_attributes_set_in_init test and use name in the error message rather than estimator since the former is more readable. --- sklearn/utils/estimator_checks.py | 4 ++-- sklearn/utils/tests/test_estimator_checks.py | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/sklearn/utils/estimator_checks.py b/sklearn/utils/estimator_checks.py index b5384900b4793..2a4236ac1f346 100644 --- a/sklearn/utils/estimator_checks.py +++ b/sklearn/utils/estimator_checks.py @@ -1416,8 +1416,8 @@ def check_no_fit_attributes_set_in_init(name, Estimator): "By convention, attributes ending with '_' are " 'estimated from data in scikit-learn. Consequently they ' 'should not be initialized in the constructor of an ' - 'estimator but in the fit method. Attribute {0!r} ' - 'was found in estimator {1}'.format(estimator, attr)) + 'estimator but in the fit method. Attribute {!r} ' + 'was found in estimator {}'.format(attr, name)) def check_sparsify_coefficients(name, Estimator): diff --git a/sklearn/utils/tests/test_estimator_checks.py b/sklearn/utils/tests/test_estimator_checks.py index f5ec18101c671..1d57d0b797d09 100644 --- a/sklearn/utils/tests/test_estimator_checks.py +++ b/sklearn/utils/tests/test_estimator_checks.py @@ -7,6 +7,7 @@ from sklearn.utils.testing import assert_raises_regex, assert_true from sklearn.utils.estimator_checks import check_estimator from sklearn.utils.estimator_checks import check_estimators_unfitted +from sklearn.utils.estimator_checks import check_no_fit_attributes_set_in_init from sklearn.ensemble import AdaBoostClassifier from sklearn.linear_model import MultiTaskElasticNet from sklearn.utils.validation import check_X_y, check_array @@ -154,3 +155,19 @@ def test_check_estimators_unfitted(): # check that CorrectNotFittedError inherit from either ValueError # or AttributeError check_estimators_unfitted("estimator", CorrectNotFittedErrorClassifier) + + +def test_check_no_fit_attributes_set_in_init(): + class NonConformantEstimator(object): + def __init__(self): + self.you_should_not_set_this_ = None + + msg = ("By convention, attributes ending with '_'.+" + 'should not be initialized in the constructor.+' + "Attribute 'you_should_not_set_this_' was found.+" + 'in estimator estimator_name') + + assert_raises_regex(AssertionError, msg, + check_no_fit_attributes_set_in_init, + 'estimator_name', + NonConformantEstimator)