Repository: nilearn/nilearn

Issue:
Suggestion to use warnings-as-errors with pytest in prerelease CI
> There is at least one test that would fail under the original bug conditions.

To find stuff like this in MNE we test with warnings-as-errors in pytest, and use a prerelease version of dependencies like `matplotlib`. It helps stay ahead of the curve a bit. But it would be a big change here...

I could instead change your pytest config to at least error on matplotlib deprecations, then you'd at least see them when they cut new releases. Not sure how many other failures this would trigger, it took us a bit to get to 3.7-deprecation-free compat over in MNE...

_Originally posted by @larsoner in https://github.com/nilearn/nilearn/issues/3535#issuecomment-1442270288_
            

Making sure this doesn't get lost. This would be added to our prerelease job in the testing workflow https://github.com/nilearn/nilearn/blob/c7aa9f74ec0b13f4946921d0b4b921e81a8639f1/.github/workflows/testing.yml#L106-L114


Discussion:

Resolving patch:
diff --git a/nilearn/conftest.py b/nilearn/conftest.py
--- a/nilearn/conftest.py
+++ b/nilearn/conftest.py
@@ -1,4 +1,7 @@
 """Configuration and extra fixtures for pytest."""
+import os
+import warnings
+
 import nibabel
 import numpy as np
 import pytest
@@ -79,6 +82,35 @@ def close_all():
         plt.close("all")  # takes < 1 us so just always do it
 
 
+@pytest.fixture(autouse=True)
+def warnings_as_errors():
+    """Raise errors on deprecations from external library prereleases."""
+    flag = os.environ.get("WARN_DEPRECATION_ERRORS")
+    if flag == "true":
+        with warnings.catch_warnings():
+            warnings.filterwarnings(
+                "error",
+                message=".*numpy.*|.*scipy.*|.*nibabel.*|"
+                ".*joblib.*|.*pandas.*|.*scikit-learn.*",
+                category=DeprecationWarning,
+            )
+            if matplotlib is not None:
+                warnings.filterwarnings(
+                    "error",
+                    category=matplotlib.MatplotlibDeprecationWarning,
+                )
+            # Ignore internal DeprecationWarnings that reference a dependency
+            pattern = '.*The "C" parameter.*'
+            warnings.filterwarnings(
+                "ignore",
+                message=pattern,
+                category=DeprecationWarning,
+            )
+            yield
+    else:
+        yield
+
+
 # ------------------------   RNG   ------------------------#
 
 


Test patch:
diff --git a/nilearn/mass_univariate/tests/utils.py b/nilearn/mass_univariate/tests/utils.py
--- a/nilearn/mass_univariate/tests/utils.py
+++ b/nilearn/mass_univariate/tests/utils.py
@@ -51,7 +51,7 @@ def get_tvalue_with_alternative_library(tested_vars, target_vars, covars=None):
                 current_tested_mask[j] = True
                 current_design_matrix = design_matrix[:, current_tested_mask]
                 ols_fit = OLS(current_target, current_design_matrix).fit()
-                t_values[i, j] = np.ravel(ols_fit.t_test(test_matrix).tvalue)
+                t_values[i, j] = np.squeeze(ols_fit.t_test(test_matrix).tvalue)
 
     except ImportError:  # use linalg if statsmodels is not available
         from numpy import linalg
@@ -83,7 +83,7 @@ def get_tvalue_with_alternative_library(tested_vars, target_vars, covars=None):
                     / float(n_samples - lost_dof)
                     * t_val_denom_aux
                 )
-                t_values[j, i] = np.ravel(t_val_num / t_val_denom)
+                t_values[j, i] = np.squeeze(t_val_num / t_val_denom)
 
     t_values = t_values.T
     assert t_values.shape == (n_regressors, n_descriptors)