8000 Merge pull request #16486 from anntzer/vc · matplotlib/matplotlib@3dd0367 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3dd0367

Browse files
authored
Merge pull request #16486 from anntzer/vc
Simplify validate_color, and make it slightly stricter.
2 parents e362973 + a0ff631 commit 3dd0367

File tree

3 files changed

+20
-26
lines changed

3 files changed

+20
-26
lines changed

doc/api/next_api_changes/behaviour.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,10 @@ instead.
7878
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7979
This creates the stem plot as a `.LineCollection` rather than individual
8080
`.Line2D` objects, greatly improving performance.
81+
82+
rcParams color validator is now stricter
83+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
84+
Previously, rcParams entries whose values were color-like accepted "spurious"
85+
extra letters or characters in the "middle" of the string, e.g. ``"(0, 1a, '0.5')"``
86+
would be interpreted as ``(0, 1, 0.5)``. These extra characters (including the
87+
internal quotes) now cause a ValueError to be raised.

lib/matplotlib/rcsetup.py

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -326,13 +326,9 @@ def validate_color_for_prop_cycle(s):
326326

327327
def validate_color(s):
328328
"""Return a valid color arg."""
329-
try:
329+
if isinstance(s, str):
330330
if s.lower() == 'none':
331331
return 'none'
332-
except AttributeError:
333-
pass
334-
335-
if isinstance(s, str):
336332
if len(s) == 6 or len(s) == 8:
337333
stmp = '#' + s
338334
if is_color_like(stmp):
@@ -341,25 +337,16 @@ def validate_color(s):
341337
if is_color_like(s):
342338
return s
343339

344-
# If it is still valid, it must be a tuple.
345-
colorarg = s
346-
msg = ''
347-
if s.find(',') >= 0:
348-
# get rid of grouping symbols
349-
stmp = ''.join([c for c in s if c.isdigit() or c == '.' or c == ','])
350-
vals = stmp.split(',')
351-
if len(vals) not in [3, 4]:
352-
msg = '\nColor tuples must be of length 3 or 4'
353-
else:
354-
try:
355-
colorarg = [float(val) for val in vals]
356-
except ValueError:
357-
msg = '\nCould not convert all entries to floats'
358-
359-
if not msg and is_color_like(colorarg):
360-
return colorarg
340+
# If it is still valid, it must be a tuple (as a string from matplotlibrc).
341+
try:
342+
color = ast.literal_eval(s)
343+
except (SyntaxError, ValueError):
344+
pass
345+
else:
346+
if is_color_like(color):
347+
return color
361348

362-
raise ValueError('%s does not look like a color arg%s' % (s, msg))
349+
raise ValueError(f'{s!r} does not look like a color arg')
363350

364351

365352
validate_colorlist = _listify_validator(

lib/matplotlib/tests/test_rcparams.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,16 +310,16 @@ def generate_validator_testcases(valid):
310310
('AABBCC00', '#AABBCC00'), # RGBA hex code
311311
('tab:blue', 'tab:blue'), # named color
312312
('C12', 'C12'), # color from cycle
313-
('(0, 1, 0)', [0.0, 1.0, 0.0]), # RGB tuple
313+
('(0, 1, 0)', (0.0, 1.0, 0.0)), # RGB tuple
314314
((0, 1, 0), (0, 1, 0)), # non-string version
315-
('(0, 1, 0, 1)', [0.0, 1.0, 0.0, 1.0]), # RGBA tuple
315+
('(0, 1, 0, 1)', (0.0, 1.0, 0.0, 1.0)), # RGBA tuple
316316
((0, 1, 0, 1), (0, 1, 0, 1)), # non-string version
317-
('(0, 1, "0.5")', [0.0, 1.0, 0.5]), # unusual but valid
318317
),
319318
'fail': (('tab:veryblue', ValueError), # invalid name
320319
('(0, 1)', ValueError), # tuple with length < 3
321320
('(0, 1, 0, 1, 0)', ValueError), # tuple with length > 4
322321
('(0, 1, none)', ValueError), # cannot cast none to float
322+
('(0, 1, "0.5")', ValueError), # last one not a float
323323
),
324324
},
325325
{'validator': validate_hist_bins,

0 commit comments

Comments
 (0)
0