Skip to content

Commit ff02e17

Browse files
authored
MAINT replace enable_slep006 fixture by @config_context(enable_metadata_routing=True) (scikit-learn#30038)
1 parent 7183224 commit ff02e17

16 files changed

+98
-68
lines changed

sklearn/compose/tests/test_column_transformer.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from numpy.testing import assert_allclose
1414
from scipy import sparse
1515

16+
from sklearn import config_context
1617
from sklearn.base import BaseEstimator, TransformerMixin
1718
from sklearn.compose import (
1819
ColumnTransformer,
@@ -2685,8 +2686,8 @@ def test_routing_passed_metadata_not_supported(method):
26852686
getattr(trs, method)([[1]], sample_weight=[1], prop="a")
26862687

26872688

2688-
@pytest.mark.usefixtures("enable_slep006")
26892689
@pytest.mark.parametrize("method", ["transform", "fit_transform", "fit"])
2690+
@config_context(enable_metadata_routing=True)
26902691
def test_metadata_routing_for_column_transformer(method):
26912692
"""Test that metadata is routed correctly for column transformer."""
26922693
X = np.array([[0, 1, 2], [2, 4, 6]]).T
@@ -2722,7 +2723,7 @@ def test_metadata_routing_for_column_transformer(method):
27222723
)
27232724

27242725

2725-
@pytest.mark.usefixtures("enable_slep006")
2726+
@config_context(enable_metadata_routing=True)
27262727
def test_metadata_routing_no_fit_transform():
27272728
"""Test metadata routing when the sub-estimator doesn't implement
27282729
``fit_transform``."""
@@ -2757,8 +2758,8 @@ def transform(self, X, sample_weight=None, metadata=None):
27572758
trs.fit_transform(X, y, sample_weight=sample_weight, metadata=metadata)
27582759

27592760

2760-
@pytest.mark.usefixtures("enable_slep006")
27612761
@pytest.mark.parametrize("method", ["transform", "fit_transform", "fit"])
2762+
@config_context(enable_metadata_routing=True)
27622763
def test_metadata_routing_error_for_column_transformer(method):
27632764
"""Test that the right error is raised when metadata is not requested."""
27642765
X = np.array([[0, 1, 2], [2, 4, 6]]).T
@@ -2778,15 +2779,15 @@ def test_metadata_routing_error_for_column_transformer(method):
27782779
getattr(trs, method)(X, y, sample_weight=sample_weight, metadata=metadata)
27792780

27802781

2781-
@pytest.mark.usefixtures("enable_slep006")
2782+
@config_context(enable_metadata_routing=True)
27822783
def test_get_metadata_routing_works_without_fit():
27832784
# Regression test for https://github.com/scikit-learn/scikit-learn/issues/28186
27842785
# Make sure ct.get_metadata_routing() works w/o having called fit.
27852786
ct = ColumnTransformer([("trans", ConsumingTransformer(), [0])])
27862787
ct.get_metadata_routing()
27872788

27882789

2789-
@pytest.mark.usefixtures("enable_slep006")
2790+
@config_context(enable_metadata_routing=True)
27902791
def test_remainder_request_always_present():
27912792
# Test that remainder request is always present.
27922793
ct = ColumnTransformer(
@@ -2799,7 +2800,7 @@ def test_remainder_request_always_present():
27992800
assert router.consumes("fit", ["metadata"]) == set(["metadata"])
28002801

28012802

2802-
@pytest.mark.usefixtures("enable_slep006")
2803+
@config_context(enable_metadata_routing=True)
28032804
def test_unused_transformer_request_present():
28042805
# Test that the request of a transformer is always present even when not
28052806
# used due to no selected columns.

sklearn/conftest.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from _pytest.doctest import DoctestItem
1616
from threadpoolctl import threadpool_limits
1717

18-
from sklearn import config_context, set_config
18+
from sklearn import set_config
1919
from sklearn._min_dependencies import PYTEST_MIN_VERSION
2020
from sklearn.datasets import (
2121
fetch_20newsgroups,
@@ -46,13 +46,6 @@
4646
scipy_datasets_require_network = sp_version >= parse_version("1.10")
4747

4848

49-
@pytest.fixture
50-
def enable_slep006():
51-
"""Enable SLEP006 for all tests."""
52-
with config_context(enable_metadata_routing=True):
53-
yield
54-
55-
5649
def raccoon_face_or_skip():
5750
# SciPy >= 1.10 requires network to access to get data
5851
if scipy_datasets_require_network:

sklearn/covariance/tests/test_graphical_lasso.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from numpy.testing import assert_allclose
99
from scipy import linalg
1010

11-
from sklearn import datasets
11+
from sklearn import config_context, datasets
1212
from sklearn.covariance import (
1313
GraphicalLasso,
1414
GraphicalLassoCV,
@@ -263,7 +263,7 @@ def test_graphical_lasso_cv_scores():
263263
)
264264

265265

266-
@pytest.mark.usefixtures("enable_slep006")
266+
@config_context(enable_metadata_routing=True)
267267
def test_graphical_lasso_cv_scores_with_routing(global_random_seed):
268268
"""Check that `GraphicalLassoCV` internally dispatches metadata to
269269
the splitter.

sklearn/ensemble/tests/test_stacking.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from numpy.testing import assert_array_equal
1212
from scipy import sparse
1313

14+
from sklearn import config_context
1415
from sklearn.base import BaseEstimator, ClassifierMixin, RegressorMixin, clone
1516
from sklearn.datasets import (
1617
load_breast_cancer,
@@ -920,21 +921,20 @@ def test_routing_passed_metadata_not_supported(Estimator, Child):
920921
)
921922

922923

923-
@pytest.mark.usefixtures("enable_slep006")
924924
@pytest.mark.parametrize(
925925
"Estimator, Child",
926926
[
927927
(StackingClassifier, ConsumingClassifier),
928928
(StackingRegressor, ConsumingRegressor),
929929
],
930930
)
931+
@config_context(enable_metadata_routing=True)
931932
def test_get_metadata_routing_without_fit(Estimator, Child):
932933
# Test that metadata_routing() doesn't raise when called before fit.
933934
est = Estimator([("sub_est", Child())])
934935
est.get_metadata_routing()
935936

936937

937-
@pytest.mark.usefixtures("enable_slep006")
938938
@pytest.mark.parametrize(
939939
"Estimator, Child",
940940
[
@@ -945,6 +945,7 @@ def test_get_metadata_routing_without_fit(Estimator, Child):
945945
@pytest.mark.parametrize(
946946
"prop, prop_value", [("sample_weight", np.ones(X_iris.shape[0])), ("metadata", "a")]
947947
)
948+
@config_context(enable_metadata_routing=True)
948949
def test_metadata_routing_for_stacking_estimators(Estimator, Child, prop, prop_value):
949950
"""Test that metadata is routed correctly for Stacking*."""
950951

@@ -991,14 +992,14 @@ def test_metadata_routing_for_stacking_estimators(Estimator, Child, prop, prop_v
991992
)
992993

993994

994-
@pytest.mark.usefixtures("enable_slep006")
995995
@pytest.mark.parametrize(
996996
"Estimator, Child",
997997
[
998998
(StackingClassifier, ConsumingClassifier),
999999
(StackingRegressor, ConsumingRegressor),
10001000
],
10011001
)
1002+
@config_context(enable_metadata_routing=True)
10021003
def test_metadata_routing_error_for_stacking_estimators(Estimator, Child):
10031004
"""Test that the right error is raised when metadata is not requested."""
10041005
sample_weight, metadata = np.ones(X_iris.shape[0]), "a"

sklearn/ensemble/tests/test_voting.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import numpy as np
66
import pytest
77

8-
from sklearn import datasets
8+
from sklearn import config_context, datasets
99
from sklearn.base import BaseEstimator, ClassifierMixin, clone
1010
from sklearn.datasets import make_multilabel_classification
1111
from sklearn.dummy import DummyRegressor
@@ -606,8 +606,7 @@ def test_voting_verbose(estimator, capsys):
606606
r"\[Voting\].*\(1 of 2\) Processing lr, total=.*\n"
607607
r"\[Voting\].*\(2 of 2\) Processing rf, total=.*\n$"
608608
)
609-
610-
estimator.fit(X, y)
609+
clone(estimator).fit(X, y)
611610
assert re.match(pattern, capsys.readouterr()[0])
612611

613612

@@ -712,23 +711,23 @@ def test_routing_passed_metadata_not_supported(Estimator, Child):
712711
Estimator(["clf", Child()]).fit(X, y, sample_weight=[1, 1, 1], metadata="a")
713712

714713

715-
@pytest.mark.usefixtures("enable_slep006")
716714
@pytest.mark.parametrize(
717715
"Estimator, Child",
718716
[(VotingClassifier, ConsumingClassifier), (VotingRegressor, ConsumingRegressor)],
719717
)
718+
@config_context(enable_metadata_routing=True)
720719
def test_get_metadata_routing_without_fit(Estimator, Child):
721720
# Test that metadata_routing() doesn't raise when called before fit.
722721
est = Estimator([("sub_est", Child())])
723722
est.get_metadata_routing()
724723

725724

726-
@pytest.mark.usefixtures("enable_slep006")
727725
@pytest.mark.parametrize(
728726
"Estimator, Child",
729727
[(VotingClassifier, ConsumingClassifier), (VotingRegressor, ConsumingRegressor)],
730728
)
731729
@pytest.mark.parametrize("prop", ["sample_weight", "metadata"])
730+
@config_context(enable_metadata_routing=True)
732731
def test_metadata_routing_for_voting_estimators(Estimator, Child, prop):
733732
"""Test that metadata is routed correctly for Voting*."""
734733
X = np.array([[0, 1], [2, 2], [4, 6]])
@@ -762,11 +761,11 @@ def test_metadata_routing_for_voting_estimators(Estimator, Child, prop):
762761
check_recorded_metadata(obj=sub_est, method="fit", parent="fit", **kwargs)
763762

764763

765-
@pytest.mark.usefixtures("enable_slep006")
766764
@pytest.mark.parametrize(
767765
"Estimator, Child",
768766
[(VotingClassifier, ConsumingClassifier), (VotingRegressor, ConsumingRegressor)],
769767
)
768+
@config_context(enable_metadata_routing=True)
770769
def test_metadata_routing_error_for_voting_estimators(Estimator, Child):
771770
"""Test that the right error is raised when metadata is not requested."""
772771
X = np.array([[0, 1], [2, 2], [4, 6]])

sklearn/linear_model/tests/test_coordinate_descent.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import pytest
1010
from scipy import interpolate, sparse
1111

12-
from sklearn.base import clone, is_classifier
12+
from sklearn.base import clone, config_context, is_classifier
1313
from sklearn.datasets import load_diabetes, make_regression
1414
from sklearn.exceptions import ConvergenceWarning
1515
from sklearn.linear_model import (
@@ -1638,11 +1638,11 @@ def test_cv_estimators_reject_params_with_no_routing_enabled(EstimatorCV):
16381638
estimator.fit(X, y, groups=groups)
16391639

16401640

1641-
@pytest.mark.usefixtures("enable_slep006")
16421641
@pytest.mark.parametrize(
16431642
"MultiTaskEstimatorCV",
16441643
[MultiTaskElasticNetCV, MultiTaskLassoCV],
16451644
)
1645+
@config_context(enable_metadata_routing=True)
16461646
def test_multitask_cv_estimators_with_sample_weight(MultiTaskEstimatorCV):
16471647
"""Check that for :class:`MultiTaskElasticNetCV` and
16481648
class:`MultiTaskLassoCV` if `sample_weight` is passed and the

sklearn/linear_model/tests/test_logistic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2163,7 +2163,7 @@ def test_liblinear_not_stuck():
21632163
clf.fit(X_prep, y)
21642164

21652165

2166-
@pytest.mark.usefixtures("enable_slep006")
2166+
@config_context(enable_metadata_routing=True)
21672167
def test_lr_cv_scores_differ_when_sample_weight_is_requested():
21682168
"""Test that `sample_weight` is correctly passed to the scorer in
21692169
`LogisticRegressionCV.fit` and `LogisticRegressionCV.score` by

sklearn/linear_model/tests/test_ridge.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2360,23 +2360,23 @@ def custom_multioutput_scorer(estimator, X, y):
23602360
# ======================
23612361

23622362

2363-
@pytest.mark.usefixtures("enable_slep006")
23642363
@pytest.mark.parametrize("metaestimator", [RidgeCV, RidgeClassifierCV])
2364+
@config_context(enable_metadata_routing=True)
23652365
def test_metadata_routing_with_default_scoring(metaestimator):
23662366
"""Test that `RidgeCV` or `RidgeClassifierCV` with default `scoring`
23672367
argument (`None`), don't enter into `RecursionError` when metadata is routed.
23682368
"""
23692369
metaestimator().get_metadata_routing()
23702370

23712371

2372-
@pytest.mark.usefixtures("enable_slep006")
23732372
@pytest.mark.parametrize(
23742373
"metaestimator, make_dataset",
23752374
[
23762375
(RidgeCV(), make_regression),
23772376
(RidgeClassifierCV(), make_classification),
23782377
],
23792378
)
2379+
@config_context(enable_metadata_routing=True)
23802380
def test_set_score_request_with_default_scoring(metaestimator, make_dataset):
23812381
"""Test that `set_score_request` is set within `RidgeCV.fit()` and
23822382
`RidgeClassifierCV.fit()` when using the default scoring and no

sklearn/metrics/tests/test_score_objects.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,8 +1211,8 @@ def test_scorer_set_score_request_raises(name):
12111211
scorer.set_score_request()
12121212

12131213

1214-
@pytest.mark.usefixtures("enable_slep006")
12151214
@pytest.mark.parametrize("name", get_scorer_names(), ids=get_scorer_names())
1215+
@config_context(enable_metadata_routing=True)
12161216
def test_scorer_metadata_request(name):
12171217
"""Testing metadata requests for scorers.
12181218
@@ -1262,7 +1262,7 @@ def test_scorer_metadata_request(name):
12621262
assert list(routed_params.scorer.score.keys()) == ["sample_weight"]
12631263

12641264

1265-
@pytest.mark.usefixtures("enable_slep006")
1265+
@config_context(enable_metadata_routing=True)
12661266
def test_metadata_kwarg_conflict():
12671267
"""This test makes sure the right warning is raised if the user passes
12681268
some metadata both as a constructor to make_scorer, and during __call__.
@@ -1285,7 +1285,7 @@ def test_metadata_kwarg_conflict():
12851285
scorer(lr, X, y, labels=lr.classes_)
12861286

12871287

1288-
@pytest.mark.usefixtures("enable_slep006")
1288+
@config_context(enable_metadata_routing=True)
12891289
def test_PassthroughScorer_set_score_request():
12901290
"""Test that _PassthroughScorer.set_score_request adds the correct metadata request
12911291
on itself and doesn't change its estimator's routing."""
@@ -1320,7 +1320,7 @@ def test_PassthroughScorer_set_score_request_raises_without_routing_enabled():
13201320
scorer.set_score_request(sample_weight="my_weights")
13211321

13221322

1323-
@pytest.mark.usefixtures("enable_slep006")
1323+
@config_context(enable_metadata_routing=True)
13241324
def test_multimetric_scoring_metadata_routing():
13251325
# Test that _MultimetricScorer properly routes metadata.
13261326
def score1(y_true, y_pred):

sklearn/model_selection/tests/test_classification_threshold.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import numpy as np
22
import pytest
33

4+
from sklearn import config_context
45
from sklearn.base import BaseEstimator, ClassifierMixin, clone
56
from sklearn.datasets import (
67
load_breast_cancer,
@@ -101,7 +102,7 @@ def test_fit_and_score_over_thresholds_prefit():
101102
assert_allclose(scores, [0.5, 1.0])
102103

103104

104-
@pytest.mark.usefixtures("enable_slep006")
105+
@config_context(enable_metadata_routing=True)
105106
def test_fit_and_score_over_thresholds_sample_weight():
106107
"""Check that we dispatch the sample-weight to fit and score the classifier."""
107108
X, y = load_iris(return_X_y=True)
@@ -150,8 +151,8 @@ def test_fit_and_score_over_thresholds_sample_weight():
150151
assert_allclose(scores_repeated, scores)
151152

152153

153-
@pytest.mark.usefixtures("enable_slep006")
154154
@pytest.mark.parametrize("fit_params_type", ["list", "array"])
155+
@config_context(enable_metadata_routing=True)
155156
def test_fit_and_score_over_thresholds_fit_params(fit_params_type):
156157
"""Check that we pass `fit_params` to the classifier when calling `fit`."""
157158
X, y = make_classification(n_samples=100, random_state=0)
@@ -344,8 +345,8 @@ def test_tuned_threshold_classifier_with_string_targets(response_method, metric)
344345
assert_array_equal(np.unique(y_pred), np.sort(classes))
345346

346347

347-
@pytest.mark.usefixtures("enable_slep006")
348348
@pytest.mark.parametrize("with_sample_weight", [True, False])
349+
@config_context(enable_metadata_routing=True)
349350
def test_tuned_threshold_classifier_refit(with_sample_weight, global_random_seed):
350351
"""Check the behaviour of the `refit` parameter."""
351352
rng = np.random.RandomState(global_random_seed)
@@ -396,8 +397,8 @@ def test_tuned_threshold_classifier_refit(with_sample_weight, global_random_seed
396397
assert_allclose(model.estimator_.coef_, estimator.coef_)
397398

398399

399-
@pytest.mark.usefixtures("enable_slep006")
400400
@pytest.mark.parametrize("fit_params_type", ["list", "array"])
401+
@config_context(enable_metadata_routing=True)
401402
def test_tuned_threshold_classifier_fit_params(fit_params_type):
402403
"""Check that we pass `fit_params` to the classifier when calling `fit`."""
403404
X, y = make_classification(n_samples=100, random_state=0)
@@ -412,7 +413,7 @@ def test_tuned_threshold_classifier_fit_params(fit_params_type):
412413
model.fit(X, y, **fit_params)
413414

414415

415-
@pytest.mark.usefixtures("enable_slep006")
416+
@config_context(enable_metadata_routing=True)
416417
def test_tuned_threshold_classifier_cv_zeros_sample_weights_equivalence():
417418
"""Check that passing removing some sample from the dataset `X` is
418419
equivalent to passing a `sample_weight` with a factor 0."""
@@ -579,7 +580,7 @@ def test_fixed_threshold_classifier(response_method, threshold, pos_label):
579580
)
580581

581582

582-
@pytest.mark.usefixtures("enable_slep006")
583+
@config_context(enable_metadata_routing=True)
583584
def test_fixed_threshold_classifier_metadata_routing():
584585
"""Check that everything works with metadata routing."""
585586
X, y = make_classification(random_state=0)

0 commit comments

Comments
 (0)