Skip to content

Commit 3995a26

Browse files
committed
Remove sklearn<=1.6 compatibility code (#985)
The project already requires scikit-learn>=1.7.0 (pyproject.toml, tox.ini, .circleci/config.yml), so the sklearn<=1.6 branches of every check_estimator/tags conditional were dead code. This removes them, keeping only the >=1.6 branch (the one using check_estimator(expected_failed_checks=...)): - feature_engine/tags.py: collapse the sklearn_version > 1.6 check in _return_tags(), the shared helper used across ~20 estimator classes. - 11 tests/**/test_check_estimator_*.py files: collapse each if/else on sklearn_version vs 1.6, drop the now-unused sklearn/ parse_version imports and sklearn_version variables. - tests/test_prediction/test_check_estimator_prediction.py: this file had no >=1.6 branch, only the dead <1.6 one (its own TODO already flagged this). Removing it leaves the prediction module with no test_check_estimator_from_sklearn coverage - a pre-existing gap, not introduced by this change, left as a follow-up. - tests/test_creation/test_geo_features.py: __sklearn_tags__ always exists at sklearn>=1.7, so drop the hasattr() guard around it. - tests/test_wrappers/test_sklearn_wrapper.py: also collapse the _OneHotEncoder() test helper's sparse/sparse_output branch (sklearn <1.2 compat, dead for the same reason). The separate KBinsDiscretizer(quantile_method=...) branch (sklearn<1.7) is intentionally left as-is - different threshold, out of scope here. - tests/check_estimators_with_parametrize_tests.py: delete entirely. A standalone, non-CI reference file documenting the pre-1.6 parametrize_with_checks() call signature. _more_tags()/__sklearn_tags__() method definitions are untouched: _more_tags() is feature_engine's own internal metadata/xfail-checks store (read by tests/estimator_checks/*.py), not a legacy sklearn shim, and __sklearn_tags__() is the current sklearn API. Verified: identical test suite pass/fail counts before and after (2010 passed, 114 failed - all 114 are pre-existing narwhals-migration WIP failures unrelated to this change), flake8 and mypy clean (the one remaining mypy error is pre-existing in datetime_subtraction.py, unrelated to this PR).
1 parent 66c2e9e commit 3995a26

15 files changed

Lines changed: 172 additions & 503 deletions

‎feature_engine/tags.py‎

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
import sklearn
2-
from sklearn.utils.fixes import parse_version
3-
4-
sklearn_version = parse_version(parse_version(sklearn.__version__).base_version)
5-
6-
71
def _return_tags():
82
tags = {
93
"preserves_dtype": [],
@@ -32,14 +26,13 @@ def _return_tags():
3226
},
3327
}
3428

35-
if sklearn_version > parse_version("1.6"):
36-
msg1 = "against Feature-engines design."
37-
msg2 = "Our transformers do not preserve dtype."
38-
all_fail = {
39-
"check_do_not_raise_errors_in_init_or_set_params": msg1,
40-
"check_transformer_preserve_dtypes": msg2,
41-
# TODO: investigate this test further.
42-
"check_n_features_in_after_fitting": "not sure why it fails, we do check.",
43-
}
44-
tags["_xfail_checks"].update(all_fail) # type: ignore
29+
msg1 = "against Feature-engines design."
30+
msg2 = "Our transformers do not preserve dtype."
31+
all_fail = {
32+
"check_do_not_raise_errors_in_init_or_set_params": msg1,
33+
"check_transformer_preserve_dtypes": msg2,
34+
# TODO: investigate this test further.
35+
"check_n_features_in_after_fitting": "not sure why it fails, we do check.",
36+
}
37+
tags["_xfail_checks"].update(all_fail) # type: ignore
4538
return tags

‎tests/check_estimators_with_parametrize_tests.py‎

Lines changed: 0 additions & 200 deletions
This file was deleted.

‎tests/test_creation/test_check_estimator_creation.py‎

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import pandas as pd
22
import pytest
3-
import sklearn
43
from sklearn.pipeline import Pipeline
54
from sklearn.utils.estimator_checks import check_estimator
6-
from sklearn.utils.fixes import parse_version
75

86
from feature_engine.creation import (
97
CyclicalFeatures,
@@ -18,8 +16,6 @@
1816
)
1917
from tests.estimator_checks.sklearn_check_wrapper import wrap_for_check_estimator
2018

21-
sklearn_version = parse_version(parse_version(sklearn.__version__).base_version)
22-
2319
# Estimators for sklearn's check_estimator
2420
# Note: GeoDistanceFeatures is not included here because it requires 4 specific
2521
# named coordinate columns, but sklearn's check_estimator generates test data
@@ -33,20 +29,13 @@
3329
DecisionTreeFeatures(regression=False),
3430
]
3531

36-
if sklearn_version > parse_version("1.6"):
37-
38-
@pytest.mark.parametrize("estimator", _estimators)
39-
def test_check_estimator_from_sklearn(estimator):
40-
return check_estimator(
41-
estimator=wrap_for_check_estimator(estimator),
42-
expected_failed_checks=estimator._more_tags()["_xfail_checks"],
43-
)
44-
45-
else:
4632

47-
@pytest.mark.parametrize("estimator", _estimators)
48-
def test_check_estimator_from_sklearn(estimator):
49-
return check_estimator(estimator)
33+
@pytest.mark.parametrize("estimator", _estimators)
34+
def test_check_estimator_from_sklearn(estimator):
35+
return check_estimator(
36+
estimator=wrap_for_check_estimator(estimator),
37+
expected_failed_checks=estimator._more_tags()["_xfail_checks"],
38+
)
5039

5140

5241
_estimators = [

‎tests/test_creation/test_geo_features.py‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,5 @@ def test_more_tags_and_sklearn_tags():
356356
== "transformer has mandatory parameters"
357357
)
358358

359-
# basic check for sklearn tags if available (new sklearn versions)
360-
if hasattr(transformer, "__sklearn_tags__"):
361-
tags = transformer.__sklearn_tags__()
362-
assert tags is not None
359+
tags = transformer.__sklearn_tags__()
360+
assert tags is not None

‎tests/test_discretisation/test_check_estimator_discretisers.py‎

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import numpy as np
22
import pandas as pd
33
import pytest
4-
import sklearn
54
from sklearn.pipeline import Pipeline
65
from sklearn.utils.estimator_checks import check_estimator
7-
from sklearn.utils.fixes import parse_version
86

97
from feature_engine.discretisation import (
108
ArbitraryDiscretiser,
@@ -19,9 +17,6 @@
1917
)
2018
from tests.estimator_checks.sklearn_check_wrapper import wrap_for_check_estimator
2119

22-
sklearn_version = parse_version(parse_version(sklearn.__version__).base_version)
23-
24-
2520
_estimators = [
2621
DecisionTreeDiscretiser(regression=False),
2722
EqualFrequencyDiscretiser(),
@@ -30,20 +25,13 @@
3025
GeometricWidthDiscretiser(),
3126
]
3227

33-
if sklearn_version < parse_version("1.6"):
34-
35-
@pytest.mark.parametrize("estimator", _estimators)
36-
def test_check_estimator_from_sklearn(estimator):
37-
return check_estimator(estimator)
3828

39-
else:
40-
41-
@pytest.mark.parametrize("estimator", _estimators)
42-
def test_check_estimator_from_sklearn(estimator):
43-
return check_estimator(
44-
estimator=wrap_for_check_estimator(estimator),
45-
expected_failed_checks=estimator._more_tags()["_xfail_checks"],
46-
)
29+
@pytest.mark.parametrize("estimator", _estimators)
30+
def test_check_estimator_from_sklearn(estimator):
31+
return check_estimator(
32+
estimator=wrap_for_check_estimator(estimator),
33+
expected_failed_checks=estimator._more_tags()["_xfail_checks"],
34+
)
4735

4836

4937
@pytest.mark.parametrize("estimator", _estimators)

‎tests/test_encoding/test_check_estimator_encoders.py‎

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import pandas as pd
22
import pytest
3-
import sklearn
43
from numpy import nan
54
from sklearn import clone
65
from sklearn.exceptions import NotFittedError
76
from sklearn.pipeline import Pipeline
87
from sklearn.utils.estimator_checks import check_estimator
9-
from sklearn.utils.fixes import parse_version
108

119
from feature_engine.encoding import (
1210
CountEncoder,
@@ -26,8 +24,6 @@
2624
)
2725
from tests.estimator_checks.sklearn_check_wrapper import wrap_for_check_estimator
2826

29-
sklearn_version = parse_version(parse_version(sklearn.__version__).base_version)
30-
3127
_estimators = [
3228
CountEncoder(ignore_format=True),
3329
CountFrequencyEncoder(ignore_format=True),
@@ -47,23 +43,17 @@
4743
]
4844

4945

50-
if sklearn_version < parse_version("1.6"):
51-
52-
@pytest.mark.parametrize("estimator", _estimators)
53-
def test_check_estimator_from_sklearn(estimator):
54-
return check_estimator(estimator)
46+
expected_fails = _return_tags()["_xfail_checks"]
47+
expected_fails.update({"check_estimators_nan_inf": "transformer allows NA"})
5548

56-
else:
57-
expected_fails = _return_tags()["_xfail_checks"]
58-
expected_fails.update({"check_estimators_nan_inf": "transformer allows NA"})
5949

60-
@pytest.mark.parametrize("estimator", _estimators)
61-
def test_check_estimator_from_sklearn(estimator):
62-
if estimator.__class__.__name__ != "WoEEncoder":
63-
return check_estimator(
64-
estimator=wrap_for_check_estimator(estimator),
65-
expected_failed_checks=expected_fails,
66-
)
50+
@pytest.mark.parametrize("estimator", _estimators)
51+
def test_check_estimator_from_sklearn(estimator):
52+
if estimator.__class__.__name__ != "WoEEncoder":
53+
return check_estimator(
54+
estimator=wrap_for_check_estimator(estimator),
55+
expected_failed_checks=expected_fails,
56+
)
6757

6858

6959
_estimators = [

0 commit comments

Comments
 (0)