Skip to content

MNT Enforce ruff/pygrep-hooks rules (PGH) #31226

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 6 commits into from
Apr 28, 2025
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
2 changes: 1 addition & 1 deletion benchmarks/bench_plot_fastkmeans.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ def compute_bench_2(chunks):


if __name__ == "__main__":
from mpl_toolkits.mplot3d import axes3d # noqa register the 3d projection
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d # register the 3d projection # noqa: F401

samples_range = np.linspace(50, 150, 5).astype(int)
features_range = np.linspace(150, 50000, 5).astype(int)
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/bench_plot_lasso_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ def compute_bench(samples_range, features_range):


if __name__ == "__main__":
from mpl_toolkits.mplot3d import axes3d # noqa register the 3d projection
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d # register the 3d projection # noqa: F401

samples_range = np.linspace(10, 2000, 5).astype(int)
features_range = np.linspace(10, 2000, 5).astype(int)
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/bench_plot_svd.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ def compute_bench(samples_range, features_range, n_iter=3, rank=50):


if __name__ == "__main__":
from mpl_toolkits.mplot3d import axes3d # noqa register the 3d projection
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d # register the 3d projection # noqa: F401

samples_range = np.linspace(2, 1000, 4).astype(int)
features_range = np.linspace(2, 1000, 4).astype(int)
Expand Down
2 changes: 1 addition & 1 deletion doc/api_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -1349,4 +1349,4 @@ def _get_submodule(module_name, submodule_name):
}
"""

DEPRECATED_API_REFERENCE = {} # type: ignore
DEPRECATED_API_REFERENCE = {} # type: ignore[var-annotated]
6 changes: 4 additions & 2 deletions doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,8 +769,10 @@ def reset_sklearn_config(gallery_conf, fname):

# enable experimental module so that experimental estimators can be
# discovered properly by sphinx
from sklearn.experimental import enable_iterative_imputer # noqa
from sklearn.experimental import enable_halving_search_cv # noqa
from sklearn.experimental import ( # noqa: F401
enable_halving_search_cv,
enable_iterative_imputer,
)


def make_carousel_thumbs(app, exception):
Expand Down
14 changes: 7 additions & 7 deletions doc/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def setup_working_with_text_data():

def setup_loading_other_datasets():
try:
import pandas # noqa
import pandas # noqa: F401
except ImportError:
raise SkipTest("Skipping loading_other_datasets.rst, pandas not installed")

Expand All @@ -56,43 +56,43 @@ def setup_loading_other_datasets():

def setup_compose():
try:
import pandas # noqa
import pandas # noqa: F401
except ImportError:
raise SkipTest("Skipping compose.rst, pandas not installed")


def setup_impute():
try:
import pandas # noqa
import pandas # noqa: F401
except ImportError:
raise SkipTest("Skipping impute.rst, pandas not installed")


def setup_grid_search():
try:
import pandas # noqa
import pandas # noqa: F401
except ImportError:
raise SkipTest("Skipping grid_search.rst, pandas not installed")


def setup_preprocessing():
try:
import pandas # noqa
import pandas # noqa: F401
except ImportError:
raise SkipTest("Skipping preprocessing.rst, pandas not installed")


def skip_if_matplotlib_not_installed(fname):
try:
import matplotlib # noqa
import matplotlib # noqa: F401
except ImportError:
basename = os.path.basename(fname)
raise SkipTest(f"Skipping doctests for {basename}, matplotlib not installed")


def skip_if_cupy_not_installed(fname):
try:
import cupy # noqa
import cupy # noqa: F401
except ImportError:
basename = os.path.basename(fname)
raise SkipTest(f"Skipping doctests for {basename}, cupy not installed")
Expand Down
7 changes: 4 additions & 3 deletions examples/ensemble/plot_gradient_boosting_quantile.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,12 @@ def coverage_fraction(y, y_low, y_high):
# cross-validation on the pinball loss with alpha=0.05:

# %%
from sklearn.experimental import enable_halving_search_cv # noqa
from sklearn.model_selection import HalvingRandomSearchCV
from sklearn.metrics import make_scorer
from pprint import pprint

from sklearn.experimental import enable_halving_search_cv # noqa: F401
from sklearn.metrics import make_scorer
from sklearn.model_selection import HalvingRandomSearchCV

param_grid = dict(
learning_rate=[0.05, 0.1, 0.2],
max_depth=[2, 5, 10],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
from sklearn.ensemble import RandomForestRegressor

# To use this experimental feature, we need to explicitly ask for it:
from sklearn.experimental import enable_iterative_imputer # noqa
from sklearn.experimental import enable_iterative_imputer # noqa: F401
from sklearn.impute import IterativeImputer, SimpleImputer
from sklearn.kernel_approximation import Nystroem
from sklearn.linear_model import BayesianRidge, Ridge
Expand Down
2 changes: 1 addition & 1 deletion examples/impute/plot_missing_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def add_missing_values(X_full, y_full):
from sklearn.ensemble import RandomForestRegressor

# To use the experimental IterativeImputer, we need to explicitly ask for it:
from sklearn.experimental import enable_iterative_imputer # noqa
from sklearn.experimental import enable_iterative_imputer # noqa: F401
from sklearn.impute import IterativeImputer, KNNImputer, SimpleImputer
from sklearn.model_selection import cross_val_score
from sklearn.pipeline import make_pipeline
Expand Down
2 changes: 1 addition & 1 deletion examples/miscellaneous/plot_set_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
the `set_output` method or globally by setting `set_config(transform_output="pandas")`.
For details, see
`SLEP018 <https://scikit-learn-enhancement-proposals.readthedocs.io/en/latest/slep018/proposal.html>`__.
""" # noqa
""" # noqa: CPY001

# %%
# First, we load the iris dataset as a DataFrame to demonstrate the `set_output` API.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import pandas as pd

from sklearn import datasets
from sklearn.experimental import enable_halving_search_cv # noqa
from sklearn.experimental import enable_halving_search_cv # noqa: F401
from sklearn.model_selection import GridSearchCV, HalvingGridSearchCV
from sklearn.svm import SVC

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from sklearn import datasets
from sklearn.ensemble import RandomForestClassifier
from sklearn.experimental import enable_halving_search_cv # noqa
from sklearn.experimental import enable_halving_search_cv # noqa: F401
from sklearn.model_selection import HalvingRandomSearchCV

# %%
Expand Down
28 changes: 16 additions & 12 deletions examples/release_highlights/plot_release_highlights_0_23_0.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# ruff: noqa
# ruff: noqa: CPY001
"""
========================================
Release Highlights for scikit-learn 0.23
Expand Down Expand Up @@ -35,9 +35,10 @@
# 'poisson' loss as well.

import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import PoissonRegressor

from sklearn.ensemble import HistGradientBoostingRegressor
from sklearn.linear_model import PoissonRegressor
from sklearn.model_selection import train_test_split

n_samples, n_features = 1000, 20
rng = np.random.RandomState(0)
Expand All @@ -63,11 +64,11 @@
# this feature.

from sklearn import set_config
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import OneHotEncoder, StandardScaler
from sklearn.impute import SimpleImputer
from sklearn.compose import make_column_transformer
from sklearn.impute import SimpleImputer
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import OneHotEncoder, StandardScaler

set_config(display="diagram")

Expand All @@ -94,12 +95,13 @@
# parallelism instead of relying on joblib, so the `n_jobs` parameter has no
# effect anymore. For more details on how to control the number of threads,
# please refer to our :ref:`parallelism` notes.
import scipy
import numpy as np
from sklearn.model_selection import train_test_split
import scipy

from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs
from sklearn.metrics import completeness_score
from sklearn.model_selection import train_test_split

rng = np.random.RandomState(0)
X, y = make_blobs(random_state=rng)
Expand All @@ -126,11 +128,12 @@
# example, see :ref:`sphx_glr_auto_examples_ensemble_plot_hgbt_regression.py`.
import numpy as np
from matplotlib import pyplot as plt
from sklearn.model_selection import train_test_split

from sklearn.ensemble import HistGradientBoostingRegressor

# from sklearn.inspection import plot_partial_dependence
from sklearn.inspection import PartialDependenceDisplay
from sklearn.ensemble import HistGradientBoostingRegressor
from sklearn.model_selection import train_test_split

n_samples = 500
rng = np.random.RandomState(0)
Expand Down Expand Up @@ -173,10 +176,11 @@
# The two linear regressors :class:`~sklearn.linear_model.Lasso` and
# :class:`~sklearn.linear_model.ElasticNet` now support sample weights.

from sklearn.model_selection import train_test_split
import numpy as np

from sklearn.datasets import make_regression
from sklearn.linear_model import Lasso
import numpy as np
from sklearn.model_selection import train_test_split

n_samples, n_features = 1000, 20
rng = np.random.RandomState(0)
Expand Down
25 changes: 14 additions & 11 deletions examples/release_highlights/plot_release_highlights_0_24_0.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# ruff: noqa
# ruff: noqa: CPY001, E501
"""
========================================
Release Highlights for scikit-learn 0.24
Expand Down Expand Up @@ -51,10 +51,11 @@

import numpy as np
from scipy.stats import randint
from sklearn.experimental import enable_halving_search_cv # noqa
from sklearn.model_selection import HalvingRandomSearchCV
from sklearn.ensemble import RandomForestClassifier

from sklearn.datasets import make_classification
from sklearn.ensemble import RandomForestClassifier
from sklearn.experimental import enable_halving_search_cv # noqa: F401
from sklearn.model_selection import HalvingRandomSearchCV

rng = np.random.RandomState(0)

Expand Down Expand Up @@ -118,6 +119,7 @@
# Read more in the :ref:`User guide <self_training>`.

import numpy as np

from sklearn import datasets
from sklearn.semi_supervised import SelfTrainingClassifier
from sklearn.svm import SVC
Expand All @@ -140,9 +142,9 @@
# (backward selection), based on a cross-validated score maximization.
# See the :ref:`User Guide <sequential_feature_selection>`.

from sklearn.datasets import load_iris
from sklearn.feature_selection import SequentialFeatureSelector
from sklearn.neighbors import KNeighborsClassifier
from sklearn.datasets import load_iris

X, y = load_iris(return_X_y=True, as_frame=True)
feature_names = X.columns
Expand All @@ -163,11 +165,11 @@
# :class:`~sklearn.preprocessing.PolynomialFeatures`.

from sklearn.datasets import fetch_covtype
from sklearn.pipeline import make_pipeline
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
from sklearn.kernel_approximation import PolynomialCountSketch
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import MinMaxScaler

X, y = fetch_covtype(return_X_y=True)
pipe = make_pipeline(
Expand All @@ -194,8 +196,8 @@
# prediction on a feature for each sample separately, with one line per sample.
# See the :ref:`User Guide <individual_conditional>`

from sklearn.ensemble import RandomForestRegressor
from sklearn.datasets import fetch_california_housing
from sklearn.ensemble import RandomForestRegressor

# from sklearn.inspection import plot_partial_dependence
from sklearn.inspection import PartialDependenceDisplay
Expand Down Expand Up @@ -232,10 +234,11 @@
# splitting criterion. Setting `criterion="poisson"` might be a good choice
# if your target is a count or a frequency.

from sklearn.tree import DecisionTreeRegressor
from sklearn.model_selection import train_test_split
import numpy as np

from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeRegressor

n_samples, n_features = 1000, 20
rng = np.random.RandomState(0)
X = rng.randn(n_samples, n_features)
Expand Down
9 changes: 6 additions & 3 deletions examples/release_highlights/plot_release_highlights_1_0_0.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# ruff: noqa
# ruff: noqa: CPY001
"""
=======================================
Release Highlights for scikit-learn 1.0
Expand Down Expand Up @@ -89,6 +89,7 @@
# refer to the :ref:`User Guide <spline_transformer>`.

import numpy as np

from sklearn.preprocessing import SplineTransformer

X = np.arange(5).reshape(5, 1)
Expand Down Expand Up @@ -147,9 +148,10 @@
# is used to check that the column names of the dataframe passed in
# non-:term:`fit`, such as :term:`predict`, are consistent with features in
# :term:`fit`:
from sklearn.preprocessing import StandardScaler
import pandas as pd

from sklearn.preprocessing import StandardScaler

X = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=["a", "b", "c"])
scalar = StandardScaler().fit(X)
scalar.feature_names_in_
Expand All @@ -162,9 +164,10 @@
# will be added to all other transformers in future releases. Additionally,
# :meth:`compose.ColumnTransformer.get_feature_names_out` is available to
# combine feature names of its transformers:
import pandas as pd

from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder
import pandas as pd

X = pd.DataFrame({"pet": ["dog", "cat", "fish"], "age": [3, 7, 1]})
preprocessor = ColumnTransformer(
Expand Down
Loading
Loading