Skip to content

Commit

Permalink
MNT Simplify module deprecation procedures with automatic tests (scik…
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasjpfan authored and NicolasHug committed Oct 18, 2019
1 parent 5e7e631 commit f801805
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 72 deletions.
2 changes: 1 addition & 1 deletion conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,4 @@ def pytest_runtest_teardown(item, nextitem):
# Configures pytest to ignore deprecated modules.
collect_ignore_glob = [
os.path.join(*deprecated_path.split(".")) + ".py"
for _, deprecated_path, _ in _DEPRECATED_MODULES]
for _, deprecated_path, _, _ in _DEPRECATED_MODULES]
93 changes: 55 additions & 38 deletions sklearn/_build_utils/deprecated_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,63 @@

# TODO: Remove the whole file in 0.24

# This is a set of 3-tuples consisting of
# (new_module_name, deprecated_path, correct_import_path)
_DEPRECATED_MODULES = {
('_mocking', 'sklearn.utils.mocking', 'sklearn.utils'),

('_bagging', 'sklearn.ensemble.bagging', 'sklearn.ensemble'),
('_base', 'sklearn.ensemble.base', 'sklearn.ensemble'),
('_forest', 'sklearn.ensemble.forest', 'sklearn.ensemble'),
('_gb', 'sklearn.ensemble.gradient_boosting', 'sklearn.ensemble'),
('_iforest', 'sklearn.ensemble.iforest', 'sklearn.ensemble'),
('_voting', 'sklearn.ensemble.voting', 'sklearn.ensemble'),
('_weight_boosting', 'sklearn.ensemble.weight_boosting', 'sklearn.ensemble'),
('_classes', 'sklearn.tree.tree', 'sklearn.tree'),
('_export', 'sklearn.tree.export', 'sklearn.tree'),

('_rbm', 'sklearn.neural_network.rbm', 'sklearn.neural_network'),
('_multilayer_perceptron',
'sklearn.neural_network.multilayer_perceptron', 'sklearn.neural_network'),

('_weight_vector', 'sklearn.utils.weight_vector', 'sklearn.utils'),
('_seq_dataset', 'sklearn.utils.seq_dataset', 'sklearn.utils'),
('_fast_dict', 'sklearn.utils.fast_dict', 'sklearn.utils'),
# This is a set of 4-tuples consisting of
# (new_module_name, deprecated_path, correct_import_path, importee)
# importee is used by test_import_deprecations to check for DeprecationWarnings
_DEPRECATED_MODULES = [
('_mocking', 'sklearn.utils.mocking', 'sklearn.utils',
'MockDataFrame'),

('_bagging', 'sklearn.ensemble.bagging', 'sklearn.ensemble',
'BaggingClassifier'),
('_base', 'sklearn.ensemble.base', 'sklearn.ensemble',
'BaseEnsemble'),
('_forest', 'sklearn.ensemble.forest', 'sklearn.ensemble',
'RandomForestClassifier'),
('_gb', 'sklearn.ensemble.gradient_boosting', 'sklearn.ensemble',
'GradientBoostingClassifier'),
('_iforest', 'sklearn.ensemble.iforest', 'sklearn.ensemble',
'IsolationForest'),
('_voting', 'sklearn.ensemble.voting', 'sklearn.ensemble',
'VotingClassifier'),
('_weight_boosting', 'sklearn.ensemble.weight_boosting',
'sklearn.ensemble', 'AdaBoostClassifier'),
('_classes', 'sklearn.tree.tree', 'sklearn.tree',
'DecisionTreeClassifier'),
('_export', 'sklearn.tree.export', 'sklearn.tree', 'export_graphviz'),

('_rbm', 'sklearn.neural_network.rbm', 'sklearn.neural_network',
'BernoulliRBM'),
('_multilayer_perceptron', 'sklearn.neural_network.multilayer_perceptron',
'sklearn.neural_network', 'MLPClassifier'),

('_weight_vector', 'sklearn.utils.weight_vector', 'sklearn.utils',
'WeightVector'),
('_seq_dataset', 'sklearn.utils.seq_dataset', 'sklearn.utils',
'ArrayDataset32'),
('_fast_dict', 'sklearn.utils.fast_dict', 'sklearn.utils', 'IntFloatDict'),

('_affinity_propagation', 'sklearn.cluster.affinity_propagation_',
'sklearn.cluster'),
('_bicluster', 'sklearn.cluster.bicluster', 'sklearn.cluster'),
('_birch', 'sklearn.cluster.birch', 'sklearn.cluster'),
('_dbscan', 'sklearn.cluster.dbscan_', 'sklearn.cluster'),
('_hierarchical', 'sklearn.cluster.hierarchical', 'sklearn.cluster'),
('_k_means', 'sklearn.cluster.k_means_', 'sklearn.cluster'),
('_mean_shift', 'sklearn.cluster.mean_shift_', 'sklearn.cluster'),
('_optics', 'sklearn.cluster.optics_', 'sklearn.cluster'),
('_spectral', 'sklearn.cluster.spectral', 'sklearn.cluster'),

('_base', 'sklearn.mixture.base', 'sklearn.mixture'),
'sklearn.cluster', 'AffinityPropagation'),
('_bicluster', 'sklearn.cluster.bicluster', 'sklearn.cluster',
'SpectralBiclustering'),
('_birch', 'sklearn.cluster.birch', 'sklearn.cluster', 'Birch'),
('_dbscan', 'sklearn.cluster.dbscan_', 'sklearn.cluster', 'DBSCAN'),
('_hierarchical', 'sklearn.cluster.hierarchical', 'sklearn.cluster',
'FeatureAgglomeration'),
('_k_means', 'sklearn.cluster.k_means_', 'sklearn.cluster', 'KMeans'),
('_mean_shift', 'sklearn.cluster.mean_shift_', 'sklearn.cluster',
'MeanShift'),
('_optics', 'sklearn.cluster.optics_', 'sklearn.cluster', 'OPTICS'),
('_spectral', 'sklearn.cluster.spectral', 'sklearn.cluster',
'SpectralClustering'),

('_base', 'sklearn.mixture.base', 'sklearn.mixture', 'BaseMixture'),
('_gaussian_mixture', 'sklearn.mixture.gaussian_mixture',
'sklearn.mixture'),
'sklearn.mixture', 'GaussianMixture'),
('_bayesian_mixture', 'sklearn.mixture.bayesian_mixture',
'sklearn.mixture'),
}
'sklearn.mixture', 'BayesianGaussianMixture'),
]

_FILE_CONTENT_TEMPLATE = """
# THIS FILE WAS AUTOMATICALLY GENERATED BY deprecated_modules.py
Expand All @@ -71,7 +88,7 @@ def _create_deprecated_modules_files():
deprecation warning will be raised.
"""
for (new_module_name, deprecated_path,
correct_import_path) in _DEPRECATED_MODULES:
correct_import_path, _) in _DEPRECATED_MODULES:
relative_dots = deprecated_path.count(".") * "."
deprecated_content = _FILE_CONTENT_TEMPLATE.format(
new_module_name=new_module_name,
Expand All @@ -85,7 +102,7 @@ def _create_deprecated_modules_files():

def _clean_deprecated_modules_files():
"""Removes submodules created by _create_deprecated_modules_files."""
for (_, deprecated_path, _) in _DEPRECATED_MODULES:
for _, deprecated_path, _, _ in _DEPRECATED_MODULES:
with suppress(FileNotFoundError):
_get_deprecated_path(deprecated_path).unlink()

Expand Down
38 changes: 5 additions & 33 deletions sklearn/tests/test_import_deprecations.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,19 @@
import pytest

from sklearn.utils.testing import assert_run_python_script
from sklearn._build_utils.deprecated_modules import _DEPRECATED_MODULES


# We are deprecating importing anything that isn't in an __init__ file and
# remaming most file.py into _file.py.
# This test makes sure imports are still possible but deprecated, with the
# appropriate error message.

@pytest.mark.parametrize('deprecated_path, importee', (
('sklearn.tree.tree', 'DecisionTreeClassifier'),
('sklearn.tree.export', 'export_graphviz'),

('sklearn.ensemble.base', 'BaseEnsemble'),
('sklearn.ensemble.forest', 'RandomForestClassifier'),
('sklearn.ensemble.bagging', 'BaggingClassifier'),
('sklearn.ensemble.iforest', 'IsolationForest'),
('sklearn.ensemble.weight_boosting', 'AdaBoostClassifier'),
('sklearn.ensemble.gradient_boosting', 'GradientBoostingClassifier'),
('sklearn.ensemble.voting', 'VotingClassifier'),
('sklearn.neural_network.rbm', 'BernoulliRBM'),
('sklearn.neural_network.multilayer_perceptron', 'MLPClassifier'),
('sklearn.utils.mocking', 'MockDataFrame'),
('sklearn.utils.weight_vector', 'WeightVector'),
('sklearn.utils.seq_dataset', 'ArrayDataset32'),
('sklearn.utils.fast_dict', 'IntFloatDict'),
('sklearn.cluster.affinity_propagation_', 'AffinityPropagation'),
('sklearn.cluster.bicluster', 'SpectralBiclustering'),
('sklearn.cluster.birch', 'Birch'),
('sklearn.cluster.dbscan_', 'DBSCAN'),
('sklearn.cluster.hierarchical', 'FeatureAgglomeration'),
('sklearn.cluster.k_means_', 'KMeans'),
('sklearn.cluster.mean_shift_', 'MeanShift'),
('sklearn.cluster.optics_', 'OPTICS'),
('sklearn.cluster.spectral', 'SpectralClustering'),
('sklearn.mixture.base', 'BaseMixture'),
('sklearn.mixture.bayesian_mixture', 'BayesianGaussianMixture'),
('sklearn.mixture.gaussian_mixture', 'GaussianMixture'),
))
@pytest.mark.parametrize('deprecated_path, importee', [
(deprecated_path, importee)
for _, deprecated_path, _, importee in _DEPRECATED_MODULES
])
def test_import_is_deprecated(deprecated_path, importee):
# Make sure that "from deprecated_path import importee" is still possible
# but raises a warning
Expand Down

0 comments on commit f801805

Please sign in to comment.