Skip to content

Make FontManager.score_weight less lenient. #12565

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
merged 1 commit into from
Nov 11, 2018
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
11 changes: 11 additions & 0 deletions doc/api/next_api_changes/2018-10-19-AL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
`FontManager.score_weight` is more strict with its inputs
`````````````````````````````````````````````````````````

Previously, when a weight string was passed to `FontManager.score_weight`,

- if the weight was the string representation of an integer, it would be
converted to that integer,
- otherwise, if the weight was not a standard weight name, it would be silently
replaced by a value of 500 ("normal" weight).

`FontManager.score_weight` now raises an exception on such inputs.
22 changes: 7 additions & 15 deletions lib/matplotlib/font_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from functools import lru_cache
import json
import logging
from numbers import Number
import os
from pathlib import Path
import subprocess
Expand Down Expand Up @@ -1066,8 +1067,8 @@ def score_style(self, style1, style2):
"""
if style1 == style2:
return 0.0
elif style1 in ('italic', 'oblique') and \
style2 in ('italic', 'oblique'):
elif (style1 in ('italic', 'oblique')
and style2 in ('italic', 'oblique')):
return 0.1
return 1.0

Expand Down Expand Up @@ -1111,21 +1112,12 @@ def score_weight(self, weight1, weight2):
the CSS numeric values of *weight1* and *weight2*, normalized between
0.05 and 1.0.
"""

# exact match of the weight names, e.g. weight1 == weight2 == "regular"
if (isinstance(weight1, str) and
isinstance(weight2, str) and
weight1 == weight2):
if cbook._str_equal(weight1, weight2):
return 0.0
try:
weightval1 = int(weight1)
except ValueError:
weightval1 = weight_dict.get(weight1, 500)
try:
weightval2 = int(weight2)
except ValueError:
weightval2 = weight_dict.get(weight2, 500)
return 0.95*(abs(weightval1 - weightval2) / 1000.0) + 0.05
w1 = weight1 if isinstance(weight1, Number) else weight_dict[weight1]
w2 = weight2 if isinstance(weight2, Number) else weight_dict[weight2]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree w the change. A little concerned that the error message will be mysterious? What is the actual error message here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They'd get a KeyError on the failing dict lookup, which may not be the nicest but should not be too confusing either. Frankly I'd rather wait for some version of #12232 (up to API discussion) to go in rather than add more custom error messages to this kind of things...

return 0.95 * (abs(w1 - w2) / 1000) + 0.05

def score_size(self, size1, size2):
"""
Expand Down