MNT Replace pytest.warns(None) in test_neighbors
from itertools import product
+from contextlib import nullcontext
import pytest
import re
@@ -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"]
@@ -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)
for metric_params in metric_params_list:
p = metric_params.pop("p", 2)
@@ -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(
@@ -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)
@@ -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(
