Skip to content

Commit 1fe6595

Browse files
authored
MNT Switch to absolute imports enforced by ruff (scikit-learn#31847)
1 parent da90c58 commit 1fe6595

File tree

239 files changed

+1990
-1758
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

239 files changed

+1990
-1758
lines changed

doc/developers/contributing.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1271,7 +1271,7 @@ Suppose the function ``zero_one`` is renamed to ``zero_one_loss``, we add the de
12711271
:class:`utils.deprecated` to ``zero_one`` and call ``zero_one_loss`` from that
12721272
function::
12731273

1274-
from ..utils import deprecated
1274+
from sklearn.utils import deprecated
12751275

12761276
def zero_one_loss(y_true, y_pred, normalize=True):
12771277
# actual implementation

doc/developers/develop.rst

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -660,13 +660,11 @@ In addition, we add the following guidelines:
660660
* Avoid multiple statements on one line. Prefer a line return after
661661
a control flow statement (``if``/``for``).
662662

663-
* Use relative imports for references inside scikit-learn.
663+
* Use absolute imports
664664

665-
* Unit tests are an exception to the previous rule;
666-
they should use absolute imports, exactly as client code would.
667-
A corollary is that, if ``sklearn.foo`` exports a class or function
668-
that is implemented in ``sklearn.foo.bar.baz``,
669-
the test should import it from ``sklearn.foo``.
665+
* Unit tests should use imports exactly as client code would.
666+
If ``sklearn.foo`` exports a class or function that is implemented in
667+
``sklearn.foo.bar.baz``, the test should import it from ``sklearn.foo``.
670668

671669
* **Please don't use** ``import *`` **in any case**. It is considered harmful
672670
by the `official Python recommendations

pyproject.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ preview = true
137137
# This enables us to use the explicit preview rules that we want only
138138
explicit-preview-rules = true
139139
# all rules can be found here: https://docs.astral.sh/ruff/rules/
140-
extend-select = ["E501", "W", "I", "CPY001", "PGH", "RUF"]
140+
extend-select = ["E501", "W", "I", "CPY001", "PGH", "RUF", "TID252"]
141141
ignore=[
142142
# do not assign a lambda expression, use a def
143143
"E731",
@@ -175,13 +175,16 @@ ignore=[
175175
[tool.ruff.lint.flake8-copyright]
176176
notice-rgx = "\\#\\ Authors:\\ The\\ scikit\\-learn\\ developers\\\r?\\\n\\#\\ SPDX\\-License\\-Identifier:\\ BSD\\-3\\-Clause"
177177

178+
[tool.ruff.lint.flake8-tidy-imports]
179+
ban-relative-imports = "all"
180+
178181
[tool.ruff.lint.per-file-ignores]
179182
# It's fine not to put the import at the top of the file in the examples
180183
# folder.
181184
"examples/*"=["E402"]
182185
"doc/conf.py"=["E402"]
183186
"**/tests/*"=["CPY001"]
184-
"asv_benchmarks/*"=["CPY001"]
187+
"asv_benchmarks/*"=["CPY001", "TID252"]
185188
"benchmarks/*"=["CPY001"]
186189
"doc/*"=["CPY001"]
187190
"build_tools/*"=["CPY001"]

sklearn/__check_build/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,6 @@ def raise_build_error(e):
4949

5050

5151
try:
52-
from ._check_build import check_build # noqa: F401
52+
from sklearn.__check_build._check_build import check_build # noqa: F401
5353
except ImportError as e:
5454
raise_build_error(e)

sklearn/__init__.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import os
2222
import random
2323

24-
from ._config import config_context, get_config, set_config
24+
from sklearn._config import config_context, get_config, set_config
2525

2626
logger = logging.getLogger(__name__)
2727

@@ -66,12 +66,9 @@
6666
# It is necessary to do this prior to importing show_versions as the
6767
# later is linked to the OpenMP runtime to make it possible to introspect
6868
# it and importing it first would fail if the OpenMP dll cannot be found.
69-
from . import ( # noqa: F401 E402
70-
__check_build,
71-
_distributor_init,
72-
)
73-
from .base import clone # noqa: E402
74-
from .utils._show_versions import show_versions # noqa: E402
69+
from sklearn import __check_build, _distributor_init # noqa: E402 F401
70+
from sklearn.base import clone # noqa: E402
71+
from sklearn.utils._show_versions import show_versions # noqa: E402
7572

7673
_submodules = [
7774
"calibration",

sklearn/_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ def set_config(
218218
if enable_cython_pairwise_dist is not None:
219219
local_config["enable_cython_pairwise_dist"] = enable_cython_pairwise_dist
220220
if array_api_dispatch is not None:
221-
from .utils._array_api import _check_array_api_dispatch
221+
from sklearn.utils._array_api import _check_array_api_dispatch
222222

223223
_check_array_api_dispatch(array_api_dispatch)
224224
local_config["array_api_dispatch"] = array_api_dispatch

sklearn/_loss/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# Authors: The scikit-learn developers
77
# SPDX-License-Identifier: BSD-3-Clause
88

9-
from .loss import (
9+
from sklearn._loss.loss import (
1010
AbsoluteError,
1111
HalfBinomialLoss,
1212
HalfGammaLoss,

sklearn/_loss/link.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from scipy.special import expit, logit
1313
from scipy.stats import gmean
1414

15-
from ..utils.extmath import softmax
15+
from sklearn.utils.extmath import softmax
1616

1717

1818
@dataclass

sklearn/_loss/loss.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@
2424
import numpy as np
2525
from scipy.special import xlogy
2626

27-
from ..utils import check_scalar
28-
from ..utils.stats import _weighted_percentile
29-
from ._loss import (
27+
from sklearn._loss._loss import (
3028
CyAbsoluteError,
3129
CyExponentialLoss,
3230
CyHalfBinomialLoss,
@@ -39,14 +37,16 @@
3937
CyHuberLoss,
4038
CyPinballLoss,
4139
)
42-
from .link import (
40+
from sklearn._loss.link import (
4341
HalfLogitLink,
4442
IdentityLink,
4543
Interval,
4644
LogitLink,
4745
LogLink,
4846
MultinomialLogit,
4947
)
48+
from sklearn.utils import check_scalar
49+
from sklearn.utils.stats import _weighted_percentile
5050

5151

5252
# Note: The shape of raw_prediction for multiclass classifications are

sklearn/base.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,26 @@
1313

1414
import numpy as np
1515

16-
from . import __version__
17-
from ._config import config_context, get_config
18-
from .exceptions import InconsistentVersionWarning
19-
from .utils._metadata_requests import _MetadataRequester, _routing_enabled
20-
from .utils._missing import is_scalar_nan
21-
from .utils._param_validation import validate_parameter_constraints
22-
from .utils._repr_html.base import ReprHTMLMixin, _HTMLDocumentationLinkMixin
23-
from .utils._repr_html.estimator import estimator_html_repr
24-
from .utils._repr_html.params import ParamsDict
25-
from .utils._set_output import _SetOutputMixin
26-
from .utils._tags import (
16+
from sklearn import __version__
17+
from sklearn._config import config_context, get_config
18+
from sklearn.exceptions import InconsistentVersionWarning
19+
from sklearn.utils._metadata_requests import _MetadataRequester, _routing_enabled
20+
from sklearn.utils._missing import is_scalar_nan
21+
from sklearn.utils._param_validation import validate_parameter_constraints
22+
from sklearn.utils._repr_html.base import ReprHTMLMixin, _HTMLDocumentationLinkMixin
23+
from sklearn.utils._repr_html.estimator import estimator_html_repr
24+
from sklearn.utils._repr_html.params import ParamsDict
25+
from sklearn.utils._set_output import _SetOutputMixin
26+
from sklearn.utils._tags import (
2727
ClassifierTags,
2828
RegressorTags,
2929
Tags,
3030
TargetTags,
3131
TransformerTags,
3232
get_tags,
3333
)
34-
from .utils.fixes import _IS_32BIT
35-
from .utils.validation import (
34+
from sklearn.utils.fixes import _IS_32BIT
35+
from sklearn.utils.validation import (
3636
_check_feature_names_in,
3737
_generate_get_feature_names_out,
3838
_is_fitted,
@@ -366,7 +366,7 @@ def __repr__(self, N_CHAR_MAX=700):
366366
# characters to render. We pass it as an optional parameter to ease
367367
# the tests.
368368

369-
from .utils._pprint import _EstimatorPrettyPrinter
369+
from sklearn.utils._pprint import _EstimatorPrettyPrinter
370370

371371
N_MAX_ELEMENTS_TO_SHOW = 30 # number of elements to show in sequences
372372

@@ -543,7 +543,7 @@ def score(self, X, y, sample_weight=None):
543543
score : float
544544
Mean accuracy of ``self.predict(X)`` w.r.t. `y`.
545545
"""
546-
from .metrics import accuracy_score
546+
from sklearn.metrics import accuracy_score
547547

548548
return accuracy_score(y, self.predict(X), sample_weight=sample_weight)
549549

@@ -633,7 +633,7 @@ def score(self, X, y, sample_weight=None):
633633
:class:`~sklearn.multioutput.MultiOutputRegressor`).
634634
"""
635635

636-
from .metrics import r2_score
636+
from sklearn.metrics import r2_score
637637

638638
y_pred = self.predict(X)
639639
return r2_score(y, y_pred, sample_weight=sample_weight)

0 commit comments

Comments
 (0)