Skip to content

Commit

Permalink
[FIX] force ordered output of _sanitize_reorder to match regex in t…
Browse files Browse the repository at this point in the history
…est (#3751)

* use list as order matters

* simplify regex

* use list

* prettify error message
  • Loading branch information
Remi-Gau committed Jun 13, 2023
1 parent 24055ec commit 2e9344d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
9 changes: 8 additions & 1 deletion nilearn/plotting/matrix_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,15 @@ def _sanitize_reorder(reorder):
"""Help for plot_matrix."""
VALID_REORDER_ARGS = {True, False, 'single', 'complete', 'average'}
if reorder not in VALID_REORDER_ARGS:
param_to_print = []
for item in VALID_REORDER_ARGS:
if isinstance(item, str):
param_to_print.append(f'"{item}"')
else:
param_to_print.append(str(item))
raise ValueError(
f"Parameter reorder needs to be one of {VALID_REORDER_ARGS}.")
f"Parameter reorder needs to be one of:\n{', '.join(param_to_print)}."
)
reorder = 'average' if reorder is True else reorder
return reorder

Expand Down
10 changes: 8 additions & 2 deletions nilearn/plotting/tests/test_matrix_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,15 @@ 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 "
f"one of {VALID_REORDER_VALUES}")):
match=("Parameter reorder needs to be one of:\n"
f"{', '.join(param_to_print)}.")):
_sanitize_reorder(reorder)


Expand Down

0 comments on commit 2e9344d

Please sign in to comment.