8000 Merge pull request #15278 from timhoffm/auto-backport-of-pr-15271-on-… · matplotlib/matplotlib@934ba3a · GitHub
[go: up one dir, main page]

Skip to content

Commit 934ba3a

Browse files
authored
Merge pull request #15278 from timhoffm/auto-backport-of-pr-15271-on-v3.1.x
Backport PR 8000 #15271 on branch v3.1.x (Fix font weight validation)
2 parents 101157a + 67b4ee2 commit 934ba3a

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
< 8000 /tr>
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,19 @@ def validate_fontsize(s):
461461
validate_fontsizelist = _listify_validator(validate_fontsize)
462462

463463

464+
def validate_fontweight(s):
465+
weights = [
466+
'ultralight', 'light', 'normal', 'regular', 'book', 'medium', 'roman',
467+
'semibold', 'demibold', 'demi', 'bold', 'heavy', 'extra bold', 'black']
468+
# Note: Historically, weights have been case-sensitive in Matplotlib
469+
if s in weights:
470+
return s
471+
try:
472+
return int(s)
473+
except (ValueError, TypeError):
474+
raise ValueError(f'{s} is not a valid font weight. %s')
475+
476+
464477
def validate_font_properties(s):
465478
parse_fontconfig_pattern(s)
466479
return s
@@ -1113,7 +1126,7 @@ def _validate_linestyle(ls):
11131126
'font.style': ['normal', validate_string],
11141127
'font.variant': ['normal', validate_string],
11151128
'font.stretch': ['normal', validate_string],
1116-
'font.weight': ['normal', validate_string],
1129+
'font.weight': ['normal', validate_fontweight],
11171130
'font.size': [10, validate_float], # Base font size in points
11181131
'font.serif': [['DejaVu Serif', 'Bitstream Vera Serif',
11191132
'Computer Modern Roman',
@@ -1190,7 +1203,7 @@ def _validate_linestyle(ls):
11901203

11911204
'axes.titlesize': ['large', validate_fontsize], # fontsize of the
11921205
# axes title
1193-
'axes.titleweight': ['normal', validate_string], # font weight of axes title
1206+
'axes.titleweight': ['normal', validate_fontweight], # font weight of axes title
11941207
'axes.titlepad': [6.0, validate_float], # pad from axes top to title in points
11951208
'axes.grid': [False, validate_bool], # display grid or not
11961209
'axes.grid.which': ['major', validate_axis_locator], # set whether the gid are by
@@ -1202,7 +1215,7 @@ def _validate_linestyle(ls):
12021215
'axes.labelsize': ['medium', validate_fontsize], # fontsize of the
12031216
# x any y labels
12041217
'axes.labelpad': [4.0, validate_float], # space between label and axis
1205-
'axes.labelweight': ['normal', validate_string], # fontsize of the x any y labels
1218+
'axes.labelweight': ['normal', validate_fontweight], # fontsize of the x any y labels
12061219
'axes.labelcolor': ['black', validate_color], # color of axis label
12071220
'axes.formatter.limits': [[-7, 7], validate_nseq_int(2)],
12081221
# use scientific notation if log10
@@ -1340,7 +1353,7 @@ def _validate_linestyle(ls):
13401353
## figure props
13411354
# figure title
13421355
'figure.titlesize': ['large', validate_fontsize],
1343-
'figure.titleweight': ['normal', validate_string],
1356+
'figure.titleweight': ['normal', validate_fontweight],
13441357

13451358
# figure size in inches: width by height
13461359
'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
@@ -17,6 +17,7 @@
1717
validate_colorlist,
1818
validate_color,
1919
validate_bool,
20+
validate_fontweight,
2021
validate_nseq_int,
2122
validate_nseq_float,
2223
validate_cycler,
@@ -412,6 +413,26 @@ def test_validator_invalid(validator, arg, exception_type):
412413
validator(arg)
413414

414415

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

0 commit comments

Comments
 (0)
0