8000 MNT Replace pytest.warns(None) in test_neighbors by thomasjpfan · Pull Request #23142 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

MNT Replace pytest.warns(None) in test_neighbors #23142

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 19, 2022
Merged
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
22 changes: 11 additions & 11 deletions sklearn/neighbors/tests/test_neighbors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from itertools import product
from contextlib import nullcontext

import pytest
import re
Expand Down Expand Up @@ -1529,15 +1530,14 @@ def test_neighbors_metrics(
neigh.fit(X_train)

# wminkoski is deprecated in SciPy 1.6.0 and removed in 1.8.0
ExceptionToAssert = None
if (
metric == "wminkowski"
and algorithm == "brute"
and sp_version >= parse_version("1.6.0")
):
ExceptionToAssert = FutureWarning

with pytest.warns(ExceptionToAssert):
with pytest.warns(FutureWarning):
results[algorithm] = neigh.kneighbors(X_test, return_distance=True)
else:
results[algorithm] = neigh.kneighbors(X_test, return_distance=True)

brute_dst, brute_idx = results["brute"]
Expand Down Expand Up @@ -1576,14 +1576,14 @@ def test_kneighbors_brute_backend(
metric_params_list = _generate_test_params_for(metric, n_features)

# wminkoski is deprecated in SciPy 1.6.0 and removed in 1.8.0
ExceptionToAssert = None
warn_context_manager = nullcontext()
if metric == "wminkowski" and sp_version >= parse_version("1.6.0"):
if global_dtype == np.float64:
# Warning from sklearn.metrics._dist_metrics.WMinkowskiDistance
ExceptionToAssert = FutureWarning
warn_context_manager = pytest.warns(FutureWarning)
if global_dtype == np.float32:
# Warning from Scipy
ExceptionToAssert = DeprecationWarning
warn_context_manager = pytest.warns(DeprecationWarning)
Copy link
Member Author

Choose a reason for hiding this comment

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

The CI that uses sets SKLEARN_RUN_FLOAT32_TESTS=1, has an older SciPy version so this test is not covered.

Copy link
Member

Choose a reason for hiding this comment

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

It should be covered now that we run all jobs in the azure cron job


for metric_params in metric_params_list:
p = metric_params.pop("p", 2)
Expand All @@ -1597,7 +1597,7 @@ def test_kneighbors_brute_backend(
)

neigh.fit(X_train)
with pytest.warns(ExceptionToAssert):
with warn_context_manager:
with config_context(enable_cython_pairwise_dist=False):
# Use the legacy backend for brute
legacy_brute_dst, legacy_brute_idx = neigh.kneighbors(
Expand Down Expand Up @@ -2103,9 +2103,9 @@ def test_radius_neighbors_brute_backend(
metric_params_list = _generate_test_params_for(metric, n_features)

# wminkoski is deprecated in SciPy 1.6.0 and removed in 1.8.0
ExceptionToAssert = None
warn_context_manager = nullcontext()
if metric == "wminkowski" and sp_version >= parse_version("1.6.0"):
ExceptionToAssert = FutureWarning
warn_context_manager = pytest.warns(FutureWarning)

for metric_params in metric_params_list:
p = metric_params.pop("p", 2)
Expand All @@ -2119,7 +2119,7 @@ def test_radius_neighbors_brute_backend(
)

neigh.fit(X_train)
with pytest.warns(ExceptionToAssert):
with warn_context_manager:
with config_context(enable_cython_pairwise_dist=False):
# Use the legacy backend for brute
legacy_brute_dst, legacy_brute_idx = neigh.radius_neighbors(
Expand Down
0