Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bb550e1

Browse filesBrowse files
committed
FIX: Error-handling for from_list inputs
Remove complicated check of the first item and always raise both errors using `raise e2 from e`. Check for range and monotony of values when unpacking (value, color) pairs. Combined tests using `@pytest.mark.parameterize`. Added tests for values that are not in the range of [0, 1] or that do not increase monotonically.
1 parent 850ebdb commit bb550e1
Copy full SHA for bb550e1

File tree

Expand file treeCollapse file tree

2 files changed

+26
-42
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+26
-42
lines changed

‎lib/matplotlib/colors.py

Copy file name to clipboardExpand all lines: lib/matplotlib/colors.py
+17-25Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"""
4141

4242
import base64
43-
from collections.abc import Sized, Sequence, Mapping
43+
from collections.abc import Sequence, Mapping
4444
import functools
4545
import importlib
4646
import inspect
@@ -1091,7 +1091,8 @@ def from_list(name, colors, N=256, gamma=1.0):
10911091
range :math:`[0, 1]`; i.e. 0 maps to ``colors[0]`` and 1 maps to
10921092
``colors[-1]``.
10931093
If (value, color) pairs are given, the mapping is from *value*
1094-
to *color*. This can be used to divide the range unevenly.
1094+
to *color*. This can be used to divide the range unevenly. The
1095+
values must increase monotonically from 0 to 1.
10951096
N : int
10961097
The number of RGB quantization levels.
10971098
gamma : float
@@ -1103,29 +1104,20 @@ def from_list(name, colors, N=256, gamma=1.0):
11031104
# Assume the passed colors are a list of colors
11041105
# and not a (value, color) tuple.
11051106
r, g, b, a = to_rgba_array(colors).T
1106-
except (ValueError, TypeError) as e:
1107-
# Check whether the first element is a (value, color) tuple
1108-
if (
1109-
isinstance(colors[0], Sized)
1110-
and not isinstance(colors[0], str)
1111-
and len(colors[0]) == 2
1112-
and isinstance(colors[0][0], Real)
1113-
and is_color_like(colors[0][1])
1114-
):
1115-
# Assume the passed values are a list of
1116-
# (value, color) tuples.
1117-
try:
1118-
vals, _colors = itertools.zip_longest(*colors)
1119-
except ValueError:
1120-
raise ValueError(
1121-
"colors is not a valid list of colors "
1122-
"or list of (value, color) pairs."
1123-
)
1124-
r, g, b, a = to_rgba_array(_colors).T
1125-
else:
1126-
# The first item is not a (value, color) tuple,
1127-
# so the issue might be an invalid color
1128-
raise e
1107+
except Exception as e:
1108+
# Assume the passed values are a list of
1109+
# (value, color) tuples.
1110+
try:
1111+
_vals, _colors = itertools.zip_longest(*colors)
1112+
except Exception as e2:
1113+
raise e2 from e
1114+
vals = np.asarray(_vals)
1115+
if np.min(vals) < 0 or np.max(vals) > 1 or np.any(np.diff(vals) <= 0):
1116+
raise ValueError(
1117+
"the values passed in the (value, color) pairs "
1118+
"must increase monotonically from 0 to 1."
1119+
)
1120+
r, g, b, a = to_rgba_array(_colors).T
11291121
else:
11301122
# List of value, color pairs
11311123
vals = np.linspace(0, 1, len(colors))

‎lib/matplotlib/tests/test_colors.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_colors.py
+9-17Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1740,23 +1740,15 @@ def test_lsc_from_list_color_alpha_tuple():
17401740
mcolors.LinearSegmentedColormap.from_list("testcolormap", valid_colors)
17411741

17421742

1743-
def test_lsc_from_list_mixed_tuples():
1744-
invalid_color_list = [(0.42, "blue"), (0.1, 0.1, 0.1, 0.1)]
1745-
with pytest.raises(ValueError):
1746-
mcolors.LinearSegmentedColormap.from_list("testcolormap", invalid_color_list)
1747-
color_first = ["blue", (0.42, "red")]
1748-
with pytest.raises(TypeError):
1749-
mcolors.LinearSegmentedColormap.from_list("testcolormap2", color_first)
1750-
1751-
1752-
def test_lsc_from_list_invalid_color():
1753-
invalid_color_list = [
1754-
"blue",
1755-
(0.1, 0.1, 0.1, 0.1),
1756-
("red", 2), # invalid color, see 'is_color_like'
1757-
]
1758-
with pytest.raises(ValueError):
1759-
mcolors.LinearSegmentedColormap.from_list("testcolormap", invalid_color_list)
1743+
@pytest.mark.parametrize("colors, error",
1744+
[([(0.42, "blue"), (.1, .1, .1, .1)], ValueError),
1745+
(["blue", (0.42, "red")], ValueError),
1746+
(["blue", (.1, .1, .1, .1), ("red", 2)], ValueError),
1747+
([(0, "red"), (1.1, "blue")], ValueError),
1748+
([(0.52, "red"), (0.42, "blue")], ValueError)])
1749+
def test_lsc_from_list_invalid_inputs(colors, error):
1750+
with pytest.raises(error):
1751+
mcolors.LinearSegmentedColormap.from_list("testcolormap", colors)
17601752

17611753

17621754
def test_lsc_from_list_value_color_tuple():

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.