Skip to content

Commit

Permalink
Add test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
MiguelParece committed May 6, 2024
1 parent 39fe619 commit 36e50a9
Show file tree
Hide file tree
Showing 18 changed files with 2,763 additions and 2,713 deletions.
4 changes: 3 additions & 1 deletion sklearn/ensemble/tests/test_bagging.py
Expand Up @@ -447,7 +447,9 @@ def test_error():
# Test support of decision_function
X, y = iris.data, iris.target
base = DecisionTreeClassifier()
assert not hasattr(BaggingClassifier(base).fit(X, y), "decision_function")
assert not hasattr(
BaggingClassifier(base).fit(X, y), "decision_function"
) and "decision_function" not in dir(BaggingClassifier(base).fit(X, y))


def test_parallel_classification():
Expand Down
3 changes: 3 additions & 0 deletions sklearn/ensemble/tests/test_stacking.py
Expand Up @@ -882,8 +882,11 @@ def test_stacking_final_estimator_attribute_error():
estimators=estimators, final_estimator=final_estimator, cv=3
)

assert "decision_function" not in dir(clf.fit(X, y))

outer_msg = "This 'StackingClassifier' has no attribute 'decision_function'"
inner_msg = "'RandomForestClassifier' object has no attribute 'decision_function'"

with pytest.raises(AttributeError, match=outer_msg) as exec_info:
clf.fit(X, y).decision_function(X)
assert isinstance(exec_info.value.__cause__, AttributeError)
Expand Down
4 changes: 2 additions & 2 deletions sklearn/ensemble/tests/test_voting.py
Expand Up @@ -78,9 +78,9 @@ def test_predictproba_hardvoting():
assert isinstance(exec_info.value.__cause__, AttributeError)
assert inner_msg in str(exec_info.value.__cause__)

assert not hasattr(eclf, "predict_proba")
assert not hasattr(eclf, "predict_proba") and "predict_proba" not in dir(eclf)
eclf.fit(X_scaled, y)
assert not hasattr(eclf, "predict_proba")
assert not hasattr(eclf, "predict_proba") and "predict_proba" not in dir(eclf)


def test_notfitted():
Expand Down
3 changes: 0 additions & 3 deletions sklearn/feature_selection/_from_model.py
Expand Up @@ -270,9 +270,6 @@ def __init__(
self.norm_order = norm_order
self.max_features = max_features

def __dir__(self):
return [attr for attr in super().__dir__() if hasattr(self, attr)]

def _get_support_mask(self):
estimator = getattr(self, "estimator_", self.estimator)
max_features = getattr(self, "max_features_", self.max_features)
Expand Down
1 change: 1 addition & 0 deletions sklearn/feature_selection/tests/test_rfe.py
Expand Up @@ -645,6 +645,7 @@ def test_rfe_estimator_attribute_error():

outer_msg = "This 'RFE' has no attribute 'decision_function'"
inner_msg = "'LinearRegression' object has no attribute 'decision_function'"
assert "decision_function" not in dir(rfe.fit(iris.data, iris.target))
with pytest.raises(AttributeError, match=outer_msg) as exec_info:
rfe.fit(iris.data, iris.target).decision_function(iris.data)
assert isinstance(exec_info.value.__cause__, AttributeError)
Expand Down

0 comments on commit 36e50a9

Please sign in to comment.