Skip to content

Commit

Permalink
FIX .format arguments were in the wrong order
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lesteve committed Dec 13, 2016
1 parent ae4f710 commit 5007e02
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions sklearn/utils/estimator_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
17 changes: 17 additions & 0 deletions sklearn/utils/tests/test_estimator_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

0 comments on commit 5007e02

Please sign in to comment.