8000 Backport PR #15271 on branch v3.2.x (Fix font weight validation) by meeseeksmachine · Pull Request #15276 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Backport PR #15271 on branch v3.2.x (Fix font weight validation) #15276

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions lib/matplotlib/rcsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,19 @@ def validate_fontsize(s):
validate_fontsizelist = _listify_validator(validate_fontsize)


def validate_fontweight(s):
weights = [
'ultralight', 'light', 'normal', 'regular', 'book', 'medium', 'roman',
'semibold', 'demibold', 'demi', 'bold', 'heavy', 'extra bold', 'black']
# Note: Historically, weights have been case-sensitive in Matplotlib
if s in weights:
return s
try:
return int(s)
except (ValueError, TypeError):
raise ValueError(f'{s} is not a valid font weight. %s')


def validate_font_properties(s):
parse_fontconfig_pattern(s)
return s
Expand Down Expand Up @@ -1095,7 +1108,7 @@ def _validate_linestyle(ls):
'font.style': ['normal', validate_string],
'font.variant': ['normal', validate_string],
'font.stretch': ['normal', validate_string],
'font.weight': ['normal', validate_string],
'font.weight': ['normal', validate_fontweight],
'font.size': [10, validate_float], # Base font size in points
'font.serif': [['DejaVu Serif', 'Bitstream Vera Serif',
'Computer Modern Roman',
Expand Down Expand Up @@ -1174,7 +1187,7 @@ def _validate_linestyle(ls):
'axes.titlesize': ['large', validate_fontsize], # fontsize of the
# axes title
'axes.titlelocation': ['center', validate_axes_titlelocation], # alignment of axes title
'axes.titleweight': ['normal', validate_string], # font weight of axes title
'axes.titleweight': ['normal', validate_fontweight], # font weight of axes title
'axes.titlecolor': ['auto', validate_color_or_auto], # font color of axes title
'axes.titlepad': [6.0, validate_float], # pad from axes top to title in points
'axes.grid': [False, validate_bool], # display grid or not
Expand All @@ -1187,7 +1200,7 @@ def _validate_linestyle(ls):
'axes.labelsize': ['medium', validate_fontsize], # fontsize of the
# x any y labels
'axes.labelpad': [4.0, validate_float], # space between label and axis
'axes.labelweight': ['normal', validate_string], # fontsize of the x any y labels
'axes.labelweight': ['normal', validate_fontweight], # fontsize of the x any y labels
'axes.labelcolor': ['black', validate_color], # color of axis label
'axes.formatter.limits': [[-5, 6], validate_nseq_int(2)],
# use scientific notation if log10
Expand Down Expand Up @@ -1320,7 +1333,7 @@ def _validate_linestyle(ls):
## figure props
# figure title
'figure.titlesize': ['large', validate_fontsize],
'figure.titleweight': ['normal', validate_string],
'figure.titleweight': ['normal', validate_fontweight],

# figure size in inches: width by height
'figure.figsize': [[6.4, 4.8], validate_nseq_float(2)],
Expand Down
21 changes: 21 additions & 0 deletions lib/matplotlib/tests/test_rcparams.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
validate_colorlist,
validate_color,
validate_bool,
validate_fontweight,
validate_nseq_int,
validate_nseq_float,
validate_cycler,
Expand Down Expand Up @@ -415,6 +416,26 @@ def test_validator_invalid(validator, arg, exception_type):
validator(arg)


@pytest.mark.parametrize('weight, parsed_weight', [
('bold', 'bold'),
('BOLD', ValueError), # weight is case-sensitive
(100, 100),
('100', 100),
(np.array(100), 100),
# fractional fontweights are not defined. This should actually raise a
# ValueError, but historically did not.
(20.6, 20),
('20.6', ValueError),
([100], ValueError),
])
def test_validate_fontweight(weight, parsed_weight):
if parsed_weight is ValueError:
with pytest.raises(ValueError):
validate_fontweight(weight)
else:
assert validate_fontweight(weight) == parsed_weight


def test_keymaps():
key_list = [k for k in mpl.rcParams if 'keymap' in k]
for k in key_list:
Expand Down
0