Exemple #1
0
def test_import_is_deprecated(deprecated_path, importee):
    # Make sure that "from deprecated_path import importee" is still possible
    # but raises a warning
    # We only need one entry per file, no need to check multiple imports from
    # the same file.

    # TODO: remove in 0.24

    # Special case for:
    # https://github.com/scikit-learn/scikit-learn/issues/15842
    if deprecated_path in ("sklearn.decomposition.dict_learning",
                           "sklearn.inspection.partial_dependence"):
        pytest.skip("No warning can be raised for " + deprecated_path)

    expected_message = (
        "The {deprecated_path} module is  deprecated in version "
        "0.22 and will be removed in version 0.24. "
        "The corresponding classes / functions "
        "should instead be imported from .*. "
        "Anything that cannot be imported from .* is now "
        "part of the private API.").format(deprecated_path=deprecated_path)

    script = """
    import pytest

    with pytest.warns(FutureWarning,
                      match="{expected_message}"):
        from {deprecated_path} import {importee}
    """.format(expected_message=expected_message,
               deprecated_path=deprecated_path,
               importee=importee)
    assert_run_python_script(textwrap.dedent(script))
def test_import_is_deprecated(deprecated_path, importee):
    # Make sure that "from deprecated_path import importee" is still possible
    # but raises a warning
    # We only need one entry per file, no need to check multiple imports from
    # the same file.

    # TODO: remove in 0.24

    expected_message = (
        "The {deprecated_path} module is  deprecated in version "
        "0.22 and will be removed in version 0.24. "
        "The corresponding classes / functions "
        "should instead be imported from .*. "
        "Anything that cannot be imported from .* is now "
        "part of the private API.").format(deprecated_path=deprecated_path)

    script = """
    import pytest

    with pytest.warns(DeprecationWarning,
                      match="{expected_message}"):
        from {deprecated_path} import {importee}
    """.format(expected_message=expected_message,
               deprecated_path=deprecated_path,
               importee=importee)
    assert_run_python_script(textwrap.dedent(script))
def test_import_raises_warning():
    code = """
    import pytest
    with pytest.warns(UserWarning, match="it is not needed to import"):
        from sklearn.experimental import enable_hist_gradient_boosting  # noqa
    """
    assert_run_python_script(textwrap.dedent(code))
Exemple #4
0
def test_imports_strategies():
    # Make sure different import strategies work or fail as expected.

    # Since Python caches the imported modules, we need to run a child process
    # for every test case. Else, the tests would not be independent
    # (manually removing the imports from the cache (sys.modules) is not
    # recommended and can lead to many complications).

    good_import = """
    from sklearn.experimental import enable_halving_search_cv
    from sklearn.model_selection import HalvingGridSearchCV
    from sklearn.model_selection import HalvingRandomSearchCV
    """
    assert_run_python_script(textwrap.dedent(good_import))

    good_import_with_model_selection_first = """
    import sklearn.model_selection
    from sklearn.experimental import enable_halving_search_cv
    from sklearn.model_selection import HalvingGridSearchCV
    from sklearn.model_selection import HalvingRandomSearchCV
    """
    assert_run_python_script(
        textwrap.dedent(good_import_with_model_selection_first))

    bad_imports = """
    import pytest

    with pytest.raises(ImportError):
        from sklearn.model_selection import HalvingGridSearchCV

    import sklearn.experimental
    with pytest.raises(ImportError):
        from sklearn.model_selection import HalvingGridSearchCV
    """
    assert_run_python_script(textwrap.dedent(bad_imports))
def test_imports_strategies():
    # Make sure different import strategies work or fail as expected.

    # Since Python caches the imported modules, we need to run a child process
    # for every test case. Else, the tests would not be independent
    # (manually removing the imports from the cache (sys.modules) is not
    # recommended and can lead to many complications).

    good_import = """
    from sklearn.experimental import enable_iterative_imputer
    from sklearn.impute import IterativeImputer
    """
    assert_run_python_script(textwrap.dedent(good_import))

    good_import_with_ensemble_first = """
    import sklearn.ensemble
    from sklearn.experimental import enable_iterative_imputer
    from sklearn.impute import IterativeImputer
    """
    assert_run_python_script(textwrap.dedent(good_import_with_ensemble_first))

    bad_imports = """
    import pytest

    with pytest.raises(ImportError, match='IterativeImputer is experimental'):
        from sklearn.impute import IterativeImputer

    import sklearn.experimental
    with pytest.raises(ImportError, match='IterativeImputer is experimental'):
        from sklearn.impute import IterativeImputer
    """
    assert_run_python_script(textwrap.dedent(bad_imports))
Exemple #6
0
def test_imports_strategies():
    # Make sure different import strategies work or fail as expected.

    # Since Python caches the imported modules, we need to run a child process
    # for every test case. Else, the tests would not be independent
    # (manually removing the imports from the cache (sys.modules) is not
    # recommended and can lead to many complications).

    good_import = """
    from sklearn.experimental import enable_hist_gradient_boosting
    from sklearn.ensemble import GradientBoostingClassifier
    from sklearn.ensemble import GradientBoostingRegressor
    """
    assert_run_python_script(textwrap.dedent(good_import))

    good_import_with_ensemble_first = """
    import sklearn.ensemble
    from sklearn.experimental import enable_hist_gradient_boosting
    from sklearn.ensemble import GradientBoostingClassifier
    from sklearn.ensemble import GradientBoostingRegressor
    """
    assert_run_python_script(textwrap.dedent(good_import_with_ensemble_first))

    bad_imports = """
    import pytest

    with pytest.raises(ImportError):
        from sklearn.ensemble import HistGradientBoostingClassifier

    with pytest.raises(ImportError):
        from sklearn.ensemble._hist_gradient_boosting import (
            HistGradientBoostingClassifier)

    import sklearn.experimental
    with pytest.raises(ImportError):
        from sklearn.ensemble import HistGradientBoostingClassifier
    """
    assert_run_python_script(textwrap.dedent(bad_imports))