Skip to content

MAINT cleanup utils.__init__: move IS_PYPY, _IS_WASM, _IS_32BIT into utils.fixes #28768

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions doc/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
from sklearn.datasets import get_data_home
from sklearn.datasets._base import _pkl_filepath
from sklearn.datasets._twenty_newsgroups import CACHE_NAME
from sklearn.utils import IS_PYPY
from sklearn.utils._testing import SkipTest, check_skip_network
from sklearn.utils.fixes import np_base_version, parse_version
from sklearn.utils.fixes import _IS_PYPY, np_base_version, parse_version


def setup_labeled_faces():
Expand All @@ -35,7 +34,7 @@ def setup_twenty_newsgroups():


def setup_working_with_text_data():
if IS_PYPY and os.environ.get("CI", None):
if _IS_PYPY and os.environ.get("CI", None):
raise SkipTest("Skipping too slow test with PyPy on CI")
check_skip_network()
cache_path = _pkl_filepath(get_data_home(), CACHE_NAME)
Expand Down
3 changes: 3 additions & 0 deletions doc/whats_new/v1.5.rst
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,9 @@ Changelog
:mod:`sklearn.utils`
....................

- |API| :data:`utils.IS_PYPY` is deprecated and will be removed in version 1.7.
:pr:`28768` by :user:`Jérémie du Boisberranger <jeremiedbb>`.

- |API| :func:`utils.tosequence` is deprecated and will be removed in version 1.7.
:pr:`28763` by :user:`Jérémie du Boisberranger <jeremiedbb>`.

Expand Down
3 changes: 2 additions & 1 deletion sklearn/_loss/tests/test_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
HuberLoss,
PinballLoss,
)
from sklearn.utils import _IS_WASM, assert_all_finite
from sklearn.utils import assert_all_finite
from sklearn.utils._testing import create_memmap_backed_data, skip_if_32bit
from sklearn.utils.fixes import _IS_WASM

ALL_LOSSES = list(_LOSSES.values())

Expand Down
2 changes: 1 addition & 1 deletion sklearn/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
from . import __version__
from ._config import config_context, get_config
from .exceptions import InconsistentVersionWarning
from .utils import _IS_32BIT
from .utils._estimator_html_repr import _HTMLDocumentationLinkMixin, estimator_html_repr
from .utils._metadata_requests import _MetadataRequester, _routing_enabled
from .utils._param_validation import validate_parameter_constraints
from .utils._set_output import _SetOutputMixin
from .utils._tags import (
_DEFAULT_TAGS,
)
from .utils.fixes import _IS_32BIT
from .utils.validation import (
_check_feature_names_in,
_check_y,
Expand Down
2 changes: 1 addition & 1 deletion sklearn/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
fetch_species_distributions,
)
from sklearn.tests import random_seed
from sklearn.utils import _IS_32BIT
from sklearn.utils._testing import get_pytest_filterwarning_lines
from sklearn.utils.fixes import (
_IS_32BIT,
np_base_version,
parse_version,
sp_version,
Expand Down
5 changes: 3 additions & 2 deletions sklearn/datasets/_svmlight_format_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
import scipy.sparse as sp

from .. import __version__
from ..utils import IS_PYPY, check_array
from ..utils import check_array
from ..utils._param_validation import HasMethods, Interval, StrOptions, validate_params
from ..utils.fixes import _IS_PYPY

if not IS_PYPY:
if not _IS_PYPY:
from ._svmlight_format_fast import (
_dump_svmlight_file,
_load_svmlight_file,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@
from sklearn.model_selection import cross_val_score, train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import KBinsDiscretizer, MinMaxScaler, OneHotEncoder
from sklearn.utils import _IS_32BIT, shuffle
from sklearn.utils import shuffle
from sklearn.utils._openmp_helpers import _openmp_effective_n_threads
from sklearn.utils._testing import _convert_container
from sklearn.utils.fixes import _IS_32BIT

n_threads = _openmp_effective_n_threads()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

import pytest

from sklearn.utils import _IS_WASM
from sklearn.utils._testing import assert_run_python_script_without_output
from sklearn.utils.fixes import _IS_WASM


@pytest.mark.xfail(_IS_WASM, reason="cannot start subprocess")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

import pytest

from sklearn.utils import _IS_WASM
from sklearn.utils._testing import assert_run_python_script_without_output
from sklearn.utils.fixes import _IS_WASM


@pytest.mark.xfail(_IS_WASM, reason="cannot start subprocess")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

import pytest

from sklearn.utils import _IS_WASM
from sklearn.utils._testing import assert_run_python_script_without_output
from sklearn.utils.fixes import _IS_WASM


@pytest.mark.xfail(_IS_WASM, reason="cannot start subprocess")
Expand Down
11 changes: 5 additions & 6 deletions sklearn/feature_extraction/tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@
from sklearn.model_selection import GridSearchCV, cross_val_score, train_test_split
from sklearn.pipeline import Pipeline
from sklearn.svm import LinearSVC
from sklearn.utils import _IS_WASM, IS_PYPY
from sklearn.utils._testing import (
assert_allclose_dense_sparse,
assert_almost_equal,
fails_if_pypy,
skip_if_32bit,
)
from sklearn.utils.fixes import CSC_CONTAINERS, CSR_CONTAINERS
from sklearn.utils.fixes import _IS_PYPY, _IS_WASM, CSC_CONTAINERS, CSR_CONTAINERS

JUNK_FOOD_DOCS = (
"the pizza pizza beer copyright",
Expand Down Expand Up @@ -1069,7 +1068,7 @@ def test_pickling_vectorizer():
copy = pickle.loads(s)
assert type(copy) == orig.__class__
assert copy.get_params() == orig.get_params()
if IS_PYPY and isinstance(orig, HashingVectorizer):
if _IS_PYPY and isinstance(orig, HashingVectorizer):
continue
else:
assert_allclose_dense_sparse(
Expand Down Expand Up @@ -1347,7 +1346,7 @@ def test_vectorizers_invalid_ngram_range(vec):
f"Invalid value for ngram_range={invalid_range} "
"lower boundary larger than the upper boundary."
)
if isinstance(vec, HashingVectorizer) and IS_PYPY:
if isinstance(vec, HashingVectorizer) and _IS_PYPY:
pytest.xfail(reason="HashingVectorizer is not supported on PyPy")

with pytest.raises(ValueError, match=message):
Expand Down Expand Up @@ -1462,7 +1461,7 @@ def build_preprocessor(self):
],
)
def test_callable_analyzer_error(Estimator, input_type, err_type, err_msg):
if issubclass(Estimator, HashingVectorizer) and IS_PYPY:
if issubclass(Estimator, HashingVectorizer) and _IS_PYPY:
pytest.xfail("HashingVectorizer is not supported on PyPy")
data = ["this is text, not file or filename"]
with pytest.raises(err_type, match=err_msg):
Expand Down Expand Up @@ -1495,7 +1494,7 @@ def test_callable_analyzer_reraise_error(tmpdir, Estimator):
def analyzer(doc):
raise Exception("testing")

if issubclass(Estimator, HashingVectorizer) and IS_PYPY:
if issubclass(Estimator, HashingVectorizer) and _IS_PYPY:
pytest.xfail("HashingVectorizer is not supported on PyPy")

f = tmpdir.join("file.txt")
Expand Down
2 changes: 1 addition & 1 deletion sklearn/feature_extraction/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
from ..base import BaseEstimator, OneToOneFeatureMixin, TransformerMixin, _fit_context
from ..exceptions import NotFittedError
from ..preprocessing import normalize
from ..utils import _IS_32BIT
from ..utils._param_validation import HasMethods, Interval, RealNotInt, StrOptions
from ..utils.fixes import _IS_32BIT
from ..utils.validation import FLOAT_DTYPES, check_array, check_is_fitted
from ._hash import FeatureHasher
from ._stop_words import ENGLISH_STOP_WORDS
Expand Down
2 changes: 1 addition & 1 deletion sklearn/inspection/tests/test_partial_dependence.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
)
from sklearn.tree import DecisionTreeRegressor
from sklearn.tree.tests.test_tree import assert_is_subtree
from sklearn.utils import _IS_32BIT
from sklearn.utils._testing import assert_allclose, assert_array_equal
from sklearn.utils.fixes import _IS_32BIT
from sklearn.utils.validation import check_random_state

# toy sample
Expand Down
4 changes: 2 additions & 2 deletions sklearn/linear_model/tests/test_logistic.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
)
from sklearn.preprocessing import LabelEncoder, StandardScaler, scale
from sklearn.svm import l1_min_c
from sklearn.utils import _IS_32BIT, compute_class_weight, shuffle
from sklearn.utils import compute_class_weight, shuffle
from sklearn.utils._testing import ignore_warnings, skip_if_no_parallel
from sklearn.utils.fixes import COO_CONTAINERS, CSR_CONTAINERS
from sklearn.utils.fixes import _IS_32BIT, COO_CONTAINERS, CSR_CONTAINERS

pytestmark = pytest.mark.filterwarnings(
"error::sklearn.exceptions.ConvergenceWarning:sklearn.*"
Expand Down
3 changes: 2 additions & 1 deletion sklearn/linear_model/tests/test_ridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
cross_val_predict,
)
from sklearn.preprocessing import minmax_scale
from sklearn.utils import _IS_32BIT, check_random_state
from sklearn.utils import check_random_state
from sklearn.utils._testing import (
assert_allclose,
assert_almost_equal,
Expand All @@ -48,6 +48,7 @@
ignore_warnings,
)
from sklearn.utils.fixes import (
_IS_32BIT,
COO_CONTAINERS,
CSC_CONTAINERS,
CSR_CONTAINERS,
Expand Down
5 changes: 3 additions & 2 deletions sklearn/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
StandardScaler,
)
from sklearn.semi_supervised import LabelPropagation, LabelSpreading
from sklearn.utils import _IS_WASM, IS_PYPY, all_estimators
from sklearn.utils import all_estimators
from sklearn.utils._tags import _DEFAULT_TAGS, _safe_tags
from sklearn.utils._testing import (
SkipTest,
Expand All @@ -87,6 +87,7 @@
check_transformer_get_feature_names_out_pandas,
parametrize_with_checks,
)
from sklearn.utils.fixes import _IS_PYPY, _IS_WASM


def test_all_estimator_no_base_class():
Expand Down Expand Up @@ -221,7 +222,7 @@ def test_import_all_consistency():
# Avoid test suite depending on setuptools
if "sklearn._build_utils" in modname:
continue
if IS_PYPY and (
if _IS_PYPY and (
"_svmlight_format_io" in modname
or "feature_extraction._hashing_fast" in modname
):
Expand Down
2 changes: 1 addition & 1 deletion sklearn/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import sklearn
from sklearn import config_context, get_config, set_config
from sklearn.utils import _IS_WASM
from sklearn.utils.fixes import _IS_WASM
from sklearn.utils.parallel import Parallel, delayed


Expand Down
3 changes: 2 additions & 1 deletion sklearn/tests/test_discriminant_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
_cov,
)
from sklearn.preprocessing import StandardScaler
from sklearn.utils import _IS_WASM, check_random_state
from sklearn.utils import check_random_state
from sklearn.utils._testing import (
_convert_container,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
)
from sklearn.utils.fixes import _IS_WASM

# Data is just 6 separable points in the plane
X = np.array([[-2, -1], [-1, -1], [-1, -2], [1, 1], [1, 2], [2, 1]], dtype="f")
Expand Down
6 changes: 3 additions & 3 deletions sklearn/tests/test_docstring_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
)
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import FunctionTransformer
from sklearn.utils import IS_PYPY, all_estimators
from sklearn.utils import all_estimators
from sklearn.utils._testing import (
_get_func_name,
check_docstring_parameters,
Expand All @@ -34,7 +34,7 @@
_enforce_estimator_tags_X,
_enforce_estimator_tags_y,
)
from sklearn.utils.fixes import parse_version, sp_version
from sklearn.utils.fixes import _IS_PYPY, parse_version, sp_version

# walk_packages() ignores DeprecationWarnings, now we need to ignore
# FutureWarnings
Expand Down Expand Up @@ -75,7 +75,7 @@
# Python 3.7
@pytest.mark.filterwarnings("ignore::FutureWarning")
@pytest.mark.filterwarnings("ignore::DeprecationWarning")
@pytest.mark.skipif(IS_PYPY, reason="test segfaults on PyPy")
@pytest.mark.skipif(_IS_PYPY, reason="test segfaults on PyPy")
def test_docstring_parameters():
# Test module docstring formatting

Expand Down
9 changes: 7 additions & 2 deletions sklearn/tree/tests/test_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
_check_value_ndarray,
)
from sklearn.tree._tree import Tree as CythonTree
from sklearn.utils import _IS_32BIT, compute_sample_weight
from sklearn.utils import compute_sample_weight
from sklearn.utils._testing import (
assert_almost_equal,
assert_array_almost_equal,
Expand All @@ -54,7 +54,12 @@
skip_if_32bit,
)
from sklearn.utils.estimator_checks import check_sample_weights_invariance
from sklearn.utils.fixes import COO_CONTAINERS, CSC_CONTAINERS, CSR_CONTAINERS
from sklearn.utils.fixes import (
_IS_32BIT,
COO_CONTAINERS,
CSC_CONTAINERS,
CSR_CONTAINERS,
)
from sklearn.utils.validation import check_random_state

CLF_CRITERIONS = ("gini", "log_loss")
Expand Down
18 changes: 13 additions & 5 deletions sklearn/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
The :mod:`sklearn.utils` module includes various utilities.
"""

import platform
import struct
import warnings
from collections.abc import Sequence

import numpy as np
Expand Down Expand Up @@ -81,9 +80,18 @@
"gen_even_slices",
]

IS_PYPY = platform.python_implementation() == "PyPy"
_IS_32BIT = 8 * struct.calcsize("P") == 32
_IS_WASM = platform.machine() in ["wasm32", "wasm64"]

# TODO(1.7): remove
def __getattr__(name):
if name == "IS_PYPY":
warnings.warn(
"IS_PYPY is deprecated and will be removed in 1.7.",
FutureWarning,
)
from .fixes import _IS_PYPY

return _IS_PYPY
raise AttributeError(f"module {__name__} has no attribute {name}")


# TODO(1.7): remove tosequence
Expand Down
5 changes: 3 additions & 2 deletions sklearn/utils/_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@
)

import sklearn
from sklearn.utils import _IS_32BIT, IS_PYPY
from sklearn.utils._array_api import _check_array_api_dispatch
from sklearn.utils.fixes import (
_IS_32BIT,
_IS_PYPY,
VisibleDeprecationWarning,
_in_unstable_openblas_configuration,
parse_version,
Expand Down Expand Up @@ -369,7 +370,7 @@ def set_random_state(estimator, random_state=0):
import pytest

skip_if_32bit = pytest.mark.skipif(_IS_32BIT, reason="skipped on 32bit platforms")
fails_if_pypy = pytest.mark.xfail(IS_PYPY, reason="not compatible with PyPy")
fails_if_pypy = pytest.mark.xfail(_IS_PYPY, reason="not compatible with PyPy")
fails_if_unstable_openblas = pytest.mark.xfail(
_in_unstable_openblas_configuration(),
reason="OpenBLAS is unstable for this configuration",
Expand Down
4 changes: 2 additions & 2 deletions sklearn/utils/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ def all_estimators(type_filter=None):
RegressorMixin,
TransformerMixin,
)
from . import IS_PYPY
from ._testing import ignore_warnings
from .fixes import _IS_PYPY

def is_abstract(c):
if not (hasattr(c, "__abstractmethods__")):
Expand Down Expand Up @@ -108,7 +108,7 @@ def is_abstract(c):

# TODO: Remove when FeatureHasher is implemented in PYPY
# Skips FeatureHasher for PYPY
if IS_PYPY and "feature_extraction" in module_name:
if _IS_PYPY and "feature_extraction" in module_name:
classes = [
(name, est_cls)
for name, est_cls in classes
Expand Down
Loading