8000 Merge pull request #15271 from timhoffm/validate-int-like-str · matplotlib/matplotlib@a813b92 · GitHub
[go: up one dir, main page]

Skip to content

Commit a813b92

Browse files
authored
Merge pull request #15271 from timhoffm/validate-int-like-str
Fix font weight validation
2 parents ef45aae + 18cecf7 commit a813b92

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

lib/matplotlib/rcsetup.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,19 @@ def validate_fontsize(s):
401401
validate_fontsizelist = _listify_validator(validate_fontsize)
402402

403403

404+
def validate_fontweight(s):
405+
weights = [
406+
'ultralight', 'light', 'normal', 'regular', 'book', 'medium', 'roman',
407+
'semibold', 'demibold', 'demi', 'bold', 'heavy', 'extra bold', 'black']
408+
# Note: Historically, weights have been case-sensitive in Matplotlib
409+
if s in weights:
410+
return s
411+
try:
412+
return int(s)
413+
except (ValueError, TypeError):
414+
raise ValueError(f'{s} is not a valid font weight. %s')
415+
416+
404417
def validate_font_properties(s):
405418
parse_fontconfig_pattern(s)
406419
return s
@@ -1095,7 +1108,7 @@ def _validate_linestyle(ls):
10951108
'font.style': ['normal', validate_string],
10961109
'font.variant': ['normal', validate_string],
10971110
'font.stretch': ['normal', validate_string],
1098-
'font.weight': ['normal', validate_string],
1111+
'font.weight': ['normal', validate_fontweight],
10991112
'font.size': [10, validate_float], # Base font size in points
11001113
'font.serif': [['DejaVu Serif', 'Bitstream Vera Serif',
11011114
'Computer Modern Roman',
@@ -1174,7 +1187,7 @@ def _validate_linestyle(ls):
11741187
'axes.titlesize': ['large', validate_fontsize], # fontsize of the
11751188
# axes title
11761189
'axes.titlelocation': ['center', validate_axes_titlelocation], # alignment of axes title
1177-
'axes.titleweight': ['normal', validate_string], # font weight of axes title
1190+
'axes.titleweight': ['normal', validate_fontweight], # font weight of axes title
11781191
'axes.titlecolor': ['auto', validate_color_or_auto], # font color of axes title
11791192
'axes.titlepad': [6.0, validate_float], # pad from axes top to title in points
11801193
'axes.grid': [False, validate_bool], # display grid or not
@@ -1187,7 +1200,7 @@ def _validate_linestyle(ls):
11871200
'axes.labelsize': ['medium', validate_fontsize], # fontsize of the
11881201
# x any y labels
11891202
'axes.labelpad': [4.0, validate_float], # space between label and axis
1190-
'axes.labelweight': ['normal', validate_string], # fontsize of the x any y labels
1203+
'axes.labelweight': ['normal', validate_fontweight], # fontsize of the x any y labels
11911204
'axes.labelcolor': ['black', validate_color], # color of axis label
11921205
'axes.formatter.limits': [[-5, 6], validate_nseq_int(2)],
11931206
# use scientific notation if log10
@@ -1320,7 +1333,7 @@ def _validate_linestyle(ls):
13201333
## figure props
13211334
# figure title
13221335
'figure.titlesize': ['large', validate_fontsize],
1323-
'figure.titleweight': ['normal', validate_string],
1336+
'figure.titleweight': ['normal', validate_fontweight],
13241337

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

lib/matplotlib/tests/test_rcparams.py

Lines changed: 21 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,26 @@ def test_validator_invalid(validator, arg, exception_type):
415416
validator(arg)
416417

417418

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

0 commit comments

Comments
 (0)
0