8000 Fix font weight validation · matplotlib/matplotlib@56c53a1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 56c53a1

Browse files
committed
Fix font weight validation
1 parent bb7e441 commit 56c53a1

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

lib/matplotlib/rcsetup.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from collections.abc import Iterable, Mapping
1818
from functools import partial, reduce
1919
import logging
20+
import numbers
2021
import operator
2122
import os
2223
import re
@@ -401,6 +402,20 @@ def validate_fontsize(s):
401402
validate_fontsizelist = _listify_validator(validate_fontsize)
402403

403404

405+
def validate_fontweight(s):
406+
"""Validate that *s* is a font weight, i.e. either an int or a string.
407+
"""
408+
weights = [
409+
'ultralight', 'light', 'normal', 'regular', 'book', 'medium', 'roman',
410+
'semibold', 'demibold', 'demi', 'bold', 'heavy', 'extra bold', 'black']
411+
if s in weights:
412+
return s
413+
try:
414+
return int(s)
415+
except (ValueError, TypeError):
416+
raise ValueError(f'{s} is not a valid font weight. %s')
417+
418+
404419
def validate_font_properties(s):
405420
parse_fontconfig_pattern(s)
406421
return s
@@ -1095,7 +1110,7 @@ def _validate_linestyle(ls):
10951110
'font.style': ['normal', validate_string],
10961111
'font.variant': ['normal', validate_string],
10971112
'font.stretch': ['normal', validate_string],
1098-
'font.weight': ['normal', validate_string],
1113+
'font.weight': ['normal', validate_fontweight],
10991114
'font.size': [10, validate_float], # Base font size in points
11001115
'font.serif': [['DejaVu Serif', 'Bitstream Vera Serif',
11011116
'Computer Modern Roman',
@@ -1174,7 +1189,7 @@ def _validate_linestyle(ls):
11741189
'axes.titlesize': ['large', validate_fontsize], # fontsize of the
11751190
# axes title
11761191
'axes.titlelocation': ['center', validate_axes_titlelocation], # alignment of axes title
1177-
'axes.titleweight': ['normal', validate_string], # font weight of axes title
1192+
'axes.titleweight': ['normal', validate_fontweight], # font weight of axes title
11781193
'axes.titlecolor': ['auto', validate_color_or_auto], # font color of axes title
11791194
'axes.titlepad': [6.0, validate_float], # pad from axes top to title in points
11801195
'axes.grid': [False, validate_bool], # display grid or not
@@ -1187,7 +1202,7 @@ def _validate_linestyle(ls):
11871202
'axes.labelsize': ['medium', validate_fontsize], # fontsize of the
11881203
# x any y labels
11891204
'axes.labelpad': [4.0, validate_float], # space between label and axis
1190-
'axes.labelweight': ['normal', validate_string], # fontsize of the x any y labels
1205+
'axes.labelweight': ['normal', validate_fontweight], # fontsize of the x any y labels
11911206
'axes.labelcolor': ['black', validate_color], # color of axis label
11921207
'axes.formatter.limits': [[-5, 6], validate_nseq_int(2)],
11931208
# use scientific notation if log10
@@ -1320,7 +1335,7 @@ def _validate_linestyle(ls):
13201335
## figure props
13211336
# figure title
13221337
'figure.titlesize': ['large', validate_fontsize],
1323-
'figure.titleweight': ['normal', validate_string],
1338+
'figure.titleweight': ['normal', validate_fontweight],
13241339

13251340
# figure size in inches: width by height
13261341
'figure.figsize': [[6.4, 4.8], validate_nseq_float(2)],

lib/matplotlib/tests/test_rcparams.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
validate_colorlist,
2020
validate_color,
2121
validate_bool,
22+
validate_fontweight,
2223
validate_nseq_int,
2324
validate_nseq_float,
2425
validate_cycler,
@@ -415,6 +416,25 @@ def test_validator_invalid(validator, arg, exception_type):
415416
validator(arg)
416417

417418

419+
@pytest.mark.parametrize('weight, parsed_weight', [
420+
('bold', 'bold'),
421+
(100, 100),
422+
('100', 100),
423+
(np.array(100), 100),
424+
# fractional fontweights are not defined. This should actually raise a
425+
# ValueError, but historically did not.
426+
(20.6, 20),
427+
('20.6', ValueError),
428+
([100], ValueError),
429+
])
430+
def test_validate_fontweight(weight, parsed_weight):
431+
if parsed_weight is ValueError:
432+
with pytest.raises(ValueError):
433+
validate_fontweight(weight)
434+
else:
435+
assert validate_fontweight(weight) == parsed_weight
436+
437+
418438
def test_keymaps():
419439
key_list = [k for k in mpl.rcParams if 'keymap' in k]
420440
for k in key_list:

0 commit comments

Comments
 (0)
0