Skip to content

MNT Fix E721 linting issues to do type comparisons with is #29501

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 7 commits into from
Aug 30, 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
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def load_mtpl2(n_samples=None):
df["ClaimAmount"] = df["ClaimAmount"].fillna(0)

# unquote string fields
for column_name in df.columns[df.dtypes.values == object]:
for column_name in df.columns[[t is object for t in df.dtypes.values]]:
df[column_name] = df[column_name].str.strip("'")
return df.iloc[:n_samples]

Expand Down
2 changes: 1 addition & 1 deletion sklearn/cluster/_optics.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ def fit(self, X, y=None):
Returns a fitted instance of self.
"""
dtype = bool if self.metric in PAIRWISE_BOOLEAN_FUNCTIONS else float
if dtype == bool and X.dtype != bool:
if dtype is bool and X.dtype != bool:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

X.dtype is also a type. I would expect:

Suggested change
if dtype is bool and X.dtype != bool:
if dtype is bool and X.dtype is not bool:

msg = (
"Data will be converted to boolean for"
f" metric {self.metric}, to avoid this warning,"
Expand Down
2 changes: 1 addition & 1 deletion sklearn/cluster/tests/test_dbscan.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def test_input_validation():
def test_pickle():
obj = DBSCAN()
s = pickle.dumps(obj)
assert type(pickle.loads(s)) == obj.__class__
assert type(pickle.loads(s)) is obj.__class__


def test_boundaries():
Expand Down
4 changes: 2 additions & 2 deletions sklearn/linear_model/tests/test_ridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -1020,15 +1020,15 @@ def _test_ridge_cv(sparse_container):
ridge_cv.predict(X)

assert len(ridge_cv.coef_.shape) == 1
assert type(ridge_cv.intercept_) == np.float64
assert type(ridge_cv.intercept_) is np.float64

cv = KFold(5)
ridge_cv.set_params(cv=cv)
ridge_cv.fit(X, y_diabetes)
ridge_cv.predict(X)

assert len(ridge_cv.coef_.shape) == 1
assert type(ridge_cv.intercept_) == np.float64
assert type(ridge_cv.intercept_) is np.float64


@pytest.mark.parametrize(
Expand Down
2 changes: 1 addition & 1 deletion sklearn/metrics/pairwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -2436,7 +2436,7 @@ def pairwise_distances(

dtype = bool if metric in PAIRWISE_BOOLEAN_FUNCTIONS else "infer_float"

if dtype == bool and (X.dtype != bool or (Y is not None and Y.dtype != bool)):
if dtype is bool and (X.dtype != bool or (Y is not None and Y.dtype != bool)):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

msg = "Data was converted to boolean for metric %s" % metric
warnings.warn(msg, DataConversionWarning)

Expand Down
2 changes: 1 addition & 1 deletion sklearn/model_selection/_split.py
Original file line number Diff line number Diff line change
Expand Up @@ -2940,7 +2940,7 @@ def _build_repr(self):
value = getattr(self, key, None)
if value is None and hasattr(self, "cvargs"):
value = self.cvargs.get(key, None)
if len(w) and w[0].category == FutureWarning:
if len(w) and w[0].category is FutureWarning:
# if the parameter is deprecated, don't show it
continue
finally:
Expand Down
8 changes: 4 additions & 4 deletions sklearn/model_selection/tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,10 +586,10 @@ def custom_scorer(clf, X, y):
)

# Make sure all the arrays are of np.ndarray type
assert type(cv_results["test_r2"]) == np.ndarray
assert type(cv_results["test_neg_mean_squared_error"]) == np.ndarray
assert type(cv_results["fit_time"]) == np.ndarray
assert type(cv_results["score_time"]) == np.ndarray
assert isinstance(cv_results["test_r2"], np.ndarray)
assert isinstance(cv_results["test_neg_mean_squared_error"], np.ndarray)
assert isinstance(cv_results["fit_time"], np.ndarray)
assert isinstance(cv_results["score_time"], np.ndarray)

# Ensure all the times are within sane limits
assert np.all(cv_results["fit_time"] >= 0)
Expand Down
2 changes: 1 addition & 1 deletion sklearn/utils/estimator_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1502,7 +1502,7 @@ def _apply_on_subsets(func, X):
result_by_batch = [func(batch.reshape(1, n_features)) for batch in X]

# func can output tuple (e.g. score_samples)
if type(result_full) == tuple:
if isinstance(result_full, tuple):
result_full = result_full[0]
result_by_batch = list(map(lambda x: x[0], result_by_batch))

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 @@ -1350,7 +1350,7 @@ def test_check_scalar_invalid(
include_boundaries=include_boundaries,
)
assert str(raised_error.value) == str(err_msg)
assert type(raised_error.value) == type(err_msg)
assert isinstance(raised_error.value, type(err_msg))


_psd_cases_valid = {
Expand Down