Skip to content

Commit

Permalink
API Auto generates deprecation for sklearn.utils.mocking (scikit-lear…
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasjpfan authored and jnothman committed Oct 2, 2019
1 parent 7cb5daf commit dba753a
Show file tree
Hide file tree
Showing 16 changed files with 85 additions and 13 deletions.
1 change: 1 addition & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ coverage:

ignore:
- "sklearn/externals"
- "sklearn/_build_utils"
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ source = sklearn
parallel = True
omit =
*/sklearn/externals/*
*/sklearn/_build_utils/*
*/benchmarks/*
**/setup.py
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,7 @@ _configtest.o.d
sklearn/utils/seq_dataset.pyx
sklearn/utils/seq_dataset.pxd
sklearn/linear_model/sag_fast.pyx

# deprecated paths
# TODO: Remove in 0.24
sklearn/utils/mocking.py
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ clean-ctags:
clean: clean-ctags
$(PYTHON) setup.py clean
rm -rf dist
# TODO: Remove in when all modules are removed.
$(PYTHON) sklearn/_build_utils/deprecated_modules.py

in: inplace # just a shortcut
inplace:
Expand Down
14 changes: 8 additions & 6 deletions doc/whats_new/v0.22.rst
Original file line number Diff line number Diff line change
Expand Up @@ -582,12 +582,6 @@ Changelog
:user:`Scott Sievert <stsievert>` and :user:`Nathaniel Saul <sauln>`,
and `SciPy PR 10815 <https://github.com/scipy/scipy/pull/10815>`.

- |API| The following utils have been deprecated and are now private:
- ``choose_check_classifiers_labels``
- ``enforce_estimator_tags_y``
- ``optimize.newton_cg`
- ``random.random_choice_csc``

- |Enhancement| :func:`utils.safe_indexing` accepts an ``axis`` parameter to
index array-like across rows and columns. The column indexing can be done on
NumPy array, SciPy sparse matrix, and Pandas DataFrame. An additional
Expand All @@ -602,6 +596,14 @@ Changelog
NaN to integer.
:pr:`14872` by `Roman Yurchak`_.

- |API| The following utils have been deprecated and are now private:
- ``choose_check_classifiers_labels``
- ``enforce_estimator_tags_y``
- ``mocking.MockDataFrame``
- ``mocking.CheckingClassifier``
- ``optimize.newton_cg`
- ``random.random_choice_csc``

:mod:`sklearn.isotonic`
..................................

Expand Down
55 changes: 55 additions & 0 deletions sklearn/_build_utils/deprecated_modules.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""Generates submodule to allow deprecation of submodules and keeping git
blame."""
from pathlib import Path
from contextlib import suppress

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

_FILE_CONTENT_TEMPLATE = """from .{new_module_name} import * # noqa
from {relative_dots}utils.deprecation import _raise_dep_warning_if_not_pytest
deprecated_path = '{deprecated_path}'
correct_import_path = '{correct_import_path}'
_raise_dep_warning_if_not_pytest(deprecated_path, correct_import_path)
"""


def _get_deprecated_path(deprecated_path):
deprecated_parts = deprecated_path.split(".")
deprecated_parts[-1] = deprecated_parts[-1] + ".py"
return Path(*deprecated_parts)


def _create_deprecated_modules_files():
"""Add submodules that will be deprecated. A file is created based
on the deprecated submodule's name. When this submodule is imported a
deprecation warning will be raised.
"""
for (new_module_name, deprecated_path,
correct_import_path) in _DEPRECATED_MODULES:
relative_dots = deprecated_path.count(".") * "."
deprecated_content = _FILE_CONTENT_TEMPLATE.format(
new_module_name=new_module_name,
relative_dots=relative_dots,
deprecated_path=deprecated_path,
correct_import_path=correct_import_path)

with _get_deprecated_path(deprecated_path).open('w') as f:
f.write(deprecated_content)


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


if __name__ == "__main__":
_clean_deprecated_modules_files()
2 changes: 1 addition & 1 deletion sklearn/metrics/tests/test_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from sklearn.utils.testing import assert_no_warnings
from sklearn.utils.testing import assert_warns_message
from sklearn.utils.testing import ignore_warnings
from sklearn.utils.mocking import MockDataFrame
from sklearn.utils._mocking import MockDataFrame

from sklearn.metrics import accuracy_score
from sklearn.metrics import average_precision_score
Expand Down
2 changes: 1 addition & 1 deletion sklearn/model_selection/tests/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from sklearn.utils.testing import assert_allclose
from sklearn.utils.testing import assert_almost_equal
from sklearn.utils.testing import ignore_warnings
from sklearn.utils.mocking import CheckingClassifier, MockDataFrame
from sklearn.utils._mocking import CheckingClassifier, MockDataFrame

from scipy.stats import bernoulli, expon, uniform

Expand Down
2 changes: 1 addition & 1 deletion sklearn/model_selection/tests/test_split.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from sklearn.utils.testing import assert_raise_message
from sklearn.utils.testing import ignore_warnings
from sklearn.utils.validation import _num_samples
from sklearn.utils.mocking import MockDataFrame
from sklearn.utils._mocking import MockDataFrame

from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
Expand Down
2 changes: 1 addition & 1 deletion sklearn/model_selection/tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from sklearn.utils.testing import assert_array_almost_equal
from sklearn.utils.testing import assert_array_equal
from sklearn.utils.testing import assert_allclose
from sklearn.utils.mocking import CheckingClassifier, MockDataFrame
from sklearn.utils._mocking import CheckingClassifier, MockDataFrame

from sklearn.model_selection import cross_val_score, ShuffleSplit
from sklearn.model_selection import cross_val_predict
Expand Down
5 changes: 5 additions & 0 deletions sklearn/setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import os

from sklearn._build_utils import maybe_cythonize_extensions
from sklearn._build_utils.deprecated_modules import (
_create_deprecated_modules_files
)


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

_create_deprecated_modules_files()

config = Configuration('sklearn', parent_package, top_path)

# submodules with build utilities
Expand Down
2 changes: 1 addition & 1 deletion sklearn/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from sklearn import datasets

from sklearn.base import TransformerMixin
from sklearn.utils.mocking import MockDataFrame
from sklearn.utils._mocking import MockDataFrame
import pickle


Expand Down
2 changes: 2 additions & 0 deletions sklearn/tests/test_import_deprecations.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
@pytest.mark.parametrize('deprecated_path, importee', (
('sklearn.neural_network.rbm', 'BernoulliRBM'),
('sklearn.neural_network.multilayer_perceptron', 'MLPClassifier'),
('sklearn.utils.mocking', 'MockDataFrame'),
))
def test_import_is_deprecated(deprecated_path, importee):
# Make sure that "from deprecated_path import importee" is still possible
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion sklearn/utils/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from sklearn.utils import _message_with_time, _print_elapsed_time
from sklearn.utils import get_chunk_n_rows
from sklearn.utils import is_scalar_nan
from sklearn.utils.mocking import MockDataFrame
from sklearn.utils._mocking import MockDataFrame
from sklearn import config_context

# toy array
Expand Down
2 changes: 1 addition & 1 deletion sklearn/utils/tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from sklearn.utils import as_float_array, check_array, check_symmetric
from sklearn.utils import check_X_y
from sklearn.utils import deprecated
from sklearn.utils.mocking import MockDataFrame
from sklearn.utils._mocking import MockDataFrame
from sklearn.utils.estimator_checks import _NotAnArray
from sklearn.random_projection import sparse_random_matrix
from sklearn.linear_model import ARDRegression
Expand Down

0 comments on commit dba753a

Please sign in to comment.