Skip to content

Commit d9214de

Browse files
authored
Merge pull request matplotlib#17892 from DangoMelon/earlier_color_validation
Add earlier color validation
2 parents c75fad8 + 1aab9ad commit d9214de

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

lib/matplotlib/cbook/__init__.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2268,21 +2268,38 @@ def type_name(tp):
22682268
type_name(type(v))))
22692269

22702270

2271-
def _check_in_list(_values, **kwargs):
2271+
def _check_in_list(_values, *, _print_supported_values=True, **kwargs):
22722272
"""
2273-
For each *key, value* pair in *kwargs*, check that *value* is in *_values*;
2274-
if not, raise an appropriate ValueError.
2273+
For each *key, value* pair in *kwargs*, check that *value* is in *_values*.
2274+
2275+
Parameters
2276+
----------
2277+
_values : iterable
2278+
Sequence of values to check on.
2279+
_print_supported_values : bool, default: True
2280+
Whether to print *_values* when raising ValueError.
2281+
**kwargs : dict
2282+
*key, value* pairs as keyword arguments to find in *_values*.
2283+
2284+
Raises
2285+
------
2286+
ValueError
2287+
If any *value* in *kwargs* is not found in *_values*.
22752288
22762289
Examples
22772290
--------
22782291
>>> cbook._check_in_list(["foo", "bar"], arg=arg, other_arg=other_arg)
22792292
"""
22802293
values = _values
2281-
for k, v in kwargs.items():
2282-
if v not in values:
2283-
raise ValueError(
2284-
"{!r} is not a valid value for {}; supported values are {}"
2285-
.format(v, k, ', '.join(map(repr, values))))
2294+
for key, val in kwargs.items():
2295+
if val not in values:
2296+
if _print_supported_values:
2297+
raise ValueError(
2298+
f"{val!r} is not a valid value for {key}; "
2299+
f"supported values are {', '.join(map(repr, values))}")
2300+
else:
2301+
raise ValueError(
2302+
f"{val!r} is not a valid value for {key}")
22862303

22872304

22882305
def _check_shape(_shape, **kwargs):

lib/matplotlib/lines.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from .artist import Artist, allow_rasterization
1515
from .cbook import (
1616
_to_unmasked_float_array, ls_mapper, ls_mapper_r, STEP_LOOKUP_MAP)
17+
from .colors import is_color_like, get_named_colors_mapping
1718
from .markers import MarkerStyle
1819
from .path import Path
1920
from .transforms import (
@@ -1039,6 +1040,9 @@ def set_color(self, color):
10391040
----------
10401041
color : color
10411042
"""
1043+
if not is_color_like(color) and color != 'auto':
1044+
cbook._check_in_list(get_named_colors_mapping(),
1045+
_print_supported_values=False, color=color)
10421046
self._color = color
10431047
self.stale = True
10441048

lib/matplotlib/tests/test_lines.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ def test_line_colors():
100100
fig.canvas.draw()
101101

102102

103+
def test_valid_colors():
104+
line = mlines.Line2D([], [])
105+
with pytest.raises(ValueError):
106+
line.set_color("foobar")
107+
108+
103109
def test_linestyle_variants():
104110
fig = plt.figure()
105111
ax = fig.add_subplot(1, 1, 1)

0 commit comments

Comments
 (0)