Skip to content

Commit dba753a

Browse files
thomasjpfanjnothman
authored andcommitted
API Auto generates deprecation for sklearn.utils.mocking (scikit-learn#15071)
1 parent 7cb5daf commit dba753a

File tree

16 files changed

+85
-13
lines changed

16 files changed

+85
-13
lines changed

.codecov.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ coverage:
2222

2323
ignore:
2424
- "sklearn/externals"
25+
- "sklearn/_build_utils"

.coveragerc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ source = sklearn
44
parallel = True
55
omit =
66
*/sklearn/externals/*
7+
*/sklearn/_build_utils/*
78
*/benchmarks/*
89
**/setup.py

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,7 @@ _configtest.o.d
7676
sklearn/utils/seq_dataset.pyx
7777
sklearn/utils/seq_dataset.pxd
7878
sklearn/linear_model/sag_fast.pyx
79+
80+
# deprecated paths
81+
# TODO: Remove in 0.24
82+
sklearn/utils/mocking.py

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ clean-ctags:
1818
clean: clean-ctags
1919
$(PYTHON) setup.py clean
2020
rm -rf dist
21+
# TODO: Remove in when all modules are removed.
22+
$(PYTHON) sklearn/_build_utils/deprecated_modules.py
2123

2224
in: inplace # just a shortcut
2325
inplace:

doc/whats_new/v0.22.rst

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -582,12 +582,6 @@ Changelog
582582
:user:`Scott Sievert <stsievert>` and :user:`Nathaniel Saul <sauln>`,
583583
and `SciPy PR 10815 <https://github.com/scipy/scipy/pull/10815>`.
584584

585-
- |API| The following utils have been deprecated and are now private:
586-
- ``choose_check_classifiers_labels``
587-
- ``enforce_estimator_tags_y``
588-
- ``optimize.newton_cg`
589-
- ``random.random_choice_csc``
590-
591585
- |Enhancement| :func:`utils.safe_indexing` accepts an ``axis`` parameter to
592586
index array-like across rows and columns. The column indexing can be done on
593587
NumPy array, SciPy sparse matrix, and Pandas DataFrame. An additional
@@ -602,6 +596,14 @@ Changelog
602596
NaN to integer.
603597
:pr:`14872` by `Roman Yurchak`_.
604598

599+
- |API| The following utils have been deprecated and are now private:
600+
- ``choose_check_classifiers_labels``
601+
- ``enforce_estimator_tags_y``
602+
- ``mocking.MockDataFrame``
603+
- ``mocking.CheckingClassifier``
604+
- ``optimize.newton_cg`
605+
- ``random.random_choice_csc``
606+
605607
:mod:`sklearn.isotonic`
606608
..................................
607609

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""Generates submodule to allow deprecation of submodules and keeping git
2+
blame."""
3+
from pathlib import Path
4+
from contextlib import suppress
5+
6+
# This is a set of 3-tuples consisting of
7+
# (new_module_name, deprecated_path, correct_import_path)
8+
_DEPRECATED_MODULES = {
9+
# TODO: Remove in 0.24
10+
('_mocking', 'sklearn.utils.mocking', 'sklearn.utils')
11+
}
12+
13+
_FILE_CONTENT_TEMPLATE = """from .{new_module_name} import * # noqa
14+
from {relative_dots}utils.deprecation import _raise_dep_warning_if_not_pytest
15+
16+
deprecated_path = '{deprecated_path}'
17+
correct_import_path = '{correct_import_path}'
18+
19+
_raise_dep_warning_if_not_pytest(deprecated_path, correct_import_path)
20+
"""
21+
22+
23+
def _get_deprecated_path(deprecated_path):
24+
deprecated_parts = deprecated_path.split(".")
25+
deprecated_parts[-1] = deprecated_parts[-1] + ".py"
26+
return Path(*deprecated_parts)
27+
28+
29+
def _create_deprecated_modules_files():
30+
"""Add submodules that will be deprecated. A file is created based
31+
on the deprecated submodule's name. When this submodule is imported a
32+
deprecation warning will be raised.
33+
"""
34+
for (new_module_name, deprecated_path,
35+
correct_import_path) in _DEPRECATED_MODULES:
36+
relative_dots = deprecated_path.count(".") * "."
37+
deprecated_content = _FILE_CONTENT_TEMPLATE.format(
38+
new_module_name=new_module_name,
39+
relative_dots=relative_dots,
40+
deprecated_path=deprecated_path,
41+
correct_import_path=correct_import_path)
42+
43+
with _get_deprecated_path(deprecated_path).open('w') as f:
44+
f.write(deprecated_content)
45+
46+
47+
def _clean_deprecated_modules_files():
48+
"""Removes submodules created by _create_deprecated_modules_files."""
49+
for (_, deprecated_path, _) in _DEPRECATED_MODULES:
50+
with suppress(FileNotFoundError):
51+
_get_deprecated_path(deprecated_path).unlink()
52+
53+
54+
if __name__ == "__main__":
55+
_clean_deprecated_modules_files()

sklearn/metrics/tests/test_classification.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from sklearn.utils.testing import assert_no_warnings
2323
from sklearn.utils.testing import assert_warns_message
2424
from sklearn.utils.testing import ignore_warnings
25-
from sklearn.utils.mocking import MockDataFrame
25+
from sklearn.utils._mocking import MockDataFrame
2626

2727
from sklearn.metrics import accuracy_score
2828
from sklearn.metrics import average_precision_score

sklearn/model_selection/tests/test_search.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from sklearn.utils.testing import assert_allclose
2424
from sklearn.utils.testing import assert_almost_equal
2525
from sklearn.utils.testing import ignore_warnings
26-
from sklearn.utils.mocking import CheckingClassifier, MockDataFrame
26+
from sklearn.utils._mocking import CheckingClassifier, MockDataFrame
2727

2828
from scipy.stats import bernoulli, expon, uniform
2929

sklearn/model_selection/tests/test_split.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from sklearn.utils.testing import assert_raise_message
1818
from sklearn.utils.testing import ignore_warnings
1919
from sklearn.utils.validation import _num_samples
20-
from sklearn.utils.mocking import MockDataFrame
20+
from sklearn.utils._mocking import MockDataFrame
2121

2222
from sklearn.model_selection import cross_val_score
2323
from sklearn.model_selection import KFold

sklearn/model_selection/tests/test_validation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from sklearn.utils.testing import assert_array_almost_equal
2323
from sklearn.utils.testing import assert_array_equal
2424
from sklearn.utils.testing import assert_allclose
25-
from sklearn.utils.mocking import CheckingClassifier, MockDataFrame
25+
from sklearn.utils._mocking import CheckingClassifier, MockDataFrame
2626

2727
from sklearn.model_selection import cross_val_score, ShuffleSplit
2828
from sklearn.model_selection import cross_val_predict

sklearn/setup.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import os
22

33
from sklearn._build_utils import maybe_cythonize_extensions
4+
from sklearn._build_utils.deprecated_modules import (
5+
_create_deprecated_modules_files
6+
)
47

58

69
def configuration(parent_package='', top_path=None):
@@ -11,6 +14,8 @@ def configuration(parent_package='', top_path=None):
1114
if os.name == 'posix':
1215
libraries.append('m')
1316

17+
_create_deprecated_modules_files()
18+
1419
config = Configuration('sklearn', parent_package, top_path)
1520

1621
# submodules with build utilities

sklearn/tests/test_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from sklearn import datasets
2323

2424
from sklearn.base import TransformerMixin
25-
from sklearn.utils.mocking import MockDataFrame
25+
from sklearn.utils._mocking import MockDataFrame
2626
import pickle
2727

2828

sklearn/tests/test_import_deprecations.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
@pytest.mark.parametrize('deprecated_path, importee', (
1414
('sklearn.neural_network.rbm', 'BernoulliRBM'),
1515
('sklearn.neural_network.multilayer_perceptron', 'MLPClassifier'),
16+
17+
('sklearn.utils.mocking', 'MockDataFrame'),
1618
))
1719
def test_import_is_deprecated(deprecated_path, importee):
1820
# Make sure that "from deprecated_path import importee" is still possible
File renamed without changes.

sklearn/utils/tests/test_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from sklearn.utils import _message_with_time, _print_elapsed_time
2727
from sklearn.utils import get_chunk_n_rows
2828
from sklearn.utils import is_scalar_nan
29-
from sklearn.utils.mocking import MockDataFrame
29+
from sklearn.utils._mocking import MockDataFrame
3030
from sklearn import config_context
3131

3232
# toy array

sklearn/utils/tests/test_validation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from sklearn.utils import as_float_array, check_array, check_symmetric
2525
from sklearn.utils import check_X_y
2626
from sklearn.utils import deprecated
27-
from sklearn.utils.mocking import MockDataFrame
27+
from sklearn.utils._mocking import MockDataFrame
2828
from sklearn.utils.estimator_checks import _NotAnArray
2929
from sklearn.random_projection import sparse_random_matrix
3030
from sklearn.linear_model import ARDRegression

0 commit comments

Comments
 (0)