Repository: nilearn/nilearn

Issue:
test error for order of printed argument with python 3.11
See in a few runs of CI

For [example](https://github.com/nilearn/nilearn/actions/runs/5244068680/jobs/9469564671?pr=3742#step:8:14623)

```
 FAILED nilearn/plotting/tests/test_matrix_plotting.py::test_sanitize_reorder_error[None] - AssertionError: Regex pattern did not match.
 Regex: "Parameter reorder needs to be one of {False, True, 'complete', 'single', 'average'}"
 Input: "Parameter reorder needs to be one of {False, True, 'single', 'complete', 'average'}."
FAILED nilearn/plotting/tests/test_matrix_plotting.py::test_sanitize_reorder_error[foo] - AssertionError: Regex pattern did not match.
 Regex: "Parameter reorder needs to be one of {False, True, 'complete', 'single', 'average'}"
 Input: "Parameter reorder needs to be one of {False, True, 'single', 'complete', 'average'}."
FAILED nilearn/plotting/tests/test_matrix_plotting.py::test_sanitize_reorder_error[2] - AssertionError: Regex pattern did not match.
 Regex: "Parameter reorder needs to be one of {False, True, 'complete', 'single', 'average'}"
 Input: "Parameter reorder needs to be one of {False, True, 'single', 'complete', 'average'}."
= 3 failed, 2587 passed, 7 skipped, 2 xfailed, 1311 warnings in 1693.15s (0:28:13) =
```


Discussion:
@ymzayek : I will have a look at this. 
This is a zombie "bug" 🧟 (it comes back from the dead) or a unsinkable rubber bug (it keeps coming back up).

https://github.com/nilearn/nilearn/actions/runs/5269060267/jobs/9526640379#step:8:14682
Thought: do we have to treat VALID_REORDER_ARGS (and therefore also VALID_REORDER_VALUES) variable as a set? Can't it be a list or tuple? Can we mimick what the function above that (`_sanitize_tri`) is doing?
Pretty sure that this failure is now just due to some regular expression special character escaping so I am implementing your suggestion of using re.escape from #3751.
Will change it to a set so things are consistent within the module: you are correct that it may be confusing otherwise (Future us: "why tuple here and set there?")
Yea it's true that in any case you have to escape (e.g. escape the brackets for a list). I think re.escape is good as we want to have an exact match but if it's not working for whatever reason let's just go with a regex matching the first part of that warning which is what you initially implemented
OK: if I see that test fail one more time I will go with this. Sounds way more pragmatic.
OK pragmatism will prevail...

Resolving patch:
diff --git a/nilearn/plotting/matrix_plotting.py b/nilearn/plotting/matrix_plotting.py
--- a/nilearn/plotting/matrix_plotting.py
+++ b/nilearn/plotting/matrix_plotting.py
@@ -99,7 +99,7 @@ def _sanitize_tri(tri):
 
 def _sanitize_reorder(reorder):
     """Help for plot_matrix."""
-    VALID_REORDER_ARGS = {True, False, 'single', 'complete', 'average'}
+    VALID_REORDER_ARGS = (True, False, 'single', 'complete', 'average')
     if reorder not in VALID_REORDER_ARGS:
         param_to_print = []
         for item in VALID_REORDER_ARGS:


Test patch:
diff --git a/nilearn/plotting/tests/test_matrix_plotting.py b/nilearn/plotting/tests/test_matrix_plotting.py
--- a/nilearn/plotting/tests/test_matrix_plotting.py
+++ b/nilearn/plotting/tests/test_matrix_plotting.py
@@ -74,7 +74,7 @@ def test_sanitize_tri_error(tri):
         _sanitize_tri(tri)
 
 
-VALID_REORDER_VALUES = {True, False, 'single', 'complete', 'average'}
+VALID_REORDER_VALUES = (True, False, 'single', 'complete', 'average')
 
 
 @pytest.mark.parametrize("reorder", VALID_REORDER_VALUES)
@@ -89,15 +89,8 @@ def test_sanitize_reorder(reorder):
 @pytest.mark.parametrize("reorder", [None, "foo", 2])
 def test_sanitize_reorder_error(reorder):
     from ..matrix_plotting import _sanitize_reorder
-    param_to_print = []
-    for item in VALID_REORDER_VALUES:
-        if isinstance(item, str):
-            param_to_print.append(f'"{item}"')
-        else:
-            param_to_print.append(str(item))
     with pytest.raises(ValueError,
-                       match=("Parameter reorder needs to be one of:\\n"
-                              f"{', '.join(param_to_print)}.")):
+                       match=("Parameter reorder needs to be one of")):
         _sanitize_reorder(reorder)