From ffb91b3e6f6dfeffac814606d9eed50b7645531d Mon Sep 17 00:00:00 2001 From: Jef Myers Date: Tue, 8 Nov 2022 12:58:05 -0500 Subject: [PATCH 1/6] use repr in error message closes #21959 --- lib/matplotlib/axes/_axes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index bc0642323bc9..876496ef81f0 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -4416,7 +4416,7 @@ def invalid_shape_exception(csize, xsize): # severe failure => one may appreciate a verbose feedback. raise ValueError( f"'c' argument must be a color, a sequence of colors, " - f"or a sequence of numbers, not {c}") from err + f"or a sequence of numbers, not {c!r}") from err else: if len(colors) not in (0, 1, xsize): # NB: remember that a single color is also acceptable. From 60c3aa9717aa3166da319115632ada9c0a8fd802 Mon Sep 17 00:00:00 2001 From: Jef Myers Date: Tue, 8 Nov 2022 14:04:01 -0500 Subject: [PATCH 2/6] add test for repr error message --- lib/matplotlib/tests/test_axes.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 3699c9df133d..a01c9aa09728 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -8290,3 +8290,13 @@ def test_extent_units(): with pytest.raises(ValueError, match="set_extent did not consume all of the kwargs"): im.set_extent([2, 12, date_first, date_last], clip=False) + +def test_repr_error_message(): + def get_next_color(): + return 'blue' # currently unused + import re + with pytest.raises(ValueError, + match=(re.escape("'c' argument must be a color, a sequence of colors, or a sequence of numbers, not 'red\\n'"))): + c = 'red\n' + mpl.axes.Axes._parse_scatter_color_args( + c, None, kwargs={}, xsize=2, get_next_color_func=get_next_color) From d42182ed70438e90542004acfe41a38d1db7b2a5 Mon Sep 17 00:00:00 2001 From: Jef Myers Date: Tue, 8 Nov 2022 14:18:20 -0500 Subject: [PATCH 3/6] fix formatting --- lib/matplotlib/tests/test_axes.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index a01c9aa09728..1e64b8216918 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -8291,12 +8291,18 @@ def test_extent_units(): match="set_extent did not consume all of the kwargs"): im.set_extent([2, 12, date_first, date_last], clip=False) + def test_repr_error_message(): + def get_next_color(): return 'blue' # currently unused import re + err = re.escape( + ("'c' argument must be a color, a sequence of colors" + ", or a sequence of numbers, not 'red\\n'") + ) with pytest.raises(ValueError, - match=(re.escape("'c' argument must be a color, a sequence of colors, or a sequence of numbers, not 'red\\n'"))): + match=(err)): c = 'red\n' mpl.axes.Axes._parse_scatter_color_args( c, None, kwargs={}, xsize=2, get_next_color_func=get_next_color) From 81cf6842dbe82817c3a40592c1e20adf834ee303 Mon Sep 17 00:00:00 2001 From: Jef Myers Date: Tue, 8 Nov 2022 15:20:07 -0500 Subject: [PATCH 4/6] clean up formatting and naming --- lib/matplotlib/tests/test_axes.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 1e64b8216918..cbdc54a7a29b 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -8297,12 +8297,11 @@ def test_repr_error_message(): def get_next_color(): return 'blue' # currently unused import re - err = re.escape( - ("'c' argument must be a color, a sequence of colors" - ", or a sequence of numbers, not 'red\\n'") - ) - with pytest.raises(ValueError, - match=(err)): + msg = re.escape( + "'c' argument must be a color, a sequence of colors" + ", or a sequence of numbers, not 'red\\n'" + ) + with pytest.raises(ValueError, match=msg): c = 'red\n' mpl.axes.Axes._parse_scatter_color_args( c, None, kwargs={}, xsize=2, get_next_color_func=get_next_color) From 2b3aa5ed3e2e5ed84b8f31ce1cb9f01bb286d28a Mon Sep 17 00:00:00 2001 From: Jef Myers Date: Wed, 9 Nov 2022 06:00:00 -0500 Subject: [PATCH 5/6] add `pragma: no cover` --- lib/matplotlib/tests/test_axes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index cbdc54a7a29b..a0852338a4ca 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -8295,7 +8295,7 @@ def test_extent_units(): def test_repr_error_message(): def get_next_color(): - return 'blue' # currently unused + return 'blue' # pragma: no cover import re msg = re.escape( "'c' argument must be a color, a sequence of colors" From 4b1fe898c5cd387286e49c49bb48c679ebf375ee Mon Sep 17 00:00:00 2001 From: Jef Myers Date: Wed, 9 Nov 2022 20:58:46 -0500 Subject: [PATCH 6/6] remove import and function name a bit more explicit --- lib/matplotlib/tests/test_axes.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index a0852338a4ca..1221eb219ab4 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -8292,14 +8292,13 @@ def test_extent_units(): im.set_extent([2, 12, date_first, date_last], clip=False) -def test_repr_error_message(): +def test_scatter_color_repr_error(): def get_next_color(): return 'blue' # pragma: no cover - import re - msg = re.escape( - "'c' argument must be a color, a sequence of colors" - ", or a sequence of numbers, not 'red\\n'" + msg = ( + r"'c' argument must be a color, a sequence of colors" + r", or a sequence of numbers, not 'red\\n'" ) with pytest.raises(ValueError, match=msg): c = 'red\n'