From de67128045aea25aef813f6522ddd0b691529071 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Fri, 19 Oct 2018 15:12:28 +0200 Subject: [PATCH] Make FontManager.score_weight less lenient. --- doc/api/next_api_changes/2018-10-19-AL.rst | 11 +++++++++++ lib/matplotlib/font_manager.py | 22 +++++++--------------- 2 files changed, 18 insertions(+), 15 deletions(-) create mode 100644 doc/api/next_api_changes/2018-10-19-AL.rst diff --git a/doc/api/next_api_changes/2018-10-19-AL.rst b/doc/api/next_api_changes/2018-10-19-AL.rst new file mode 100644 index 000000000000..6717ec103175 --- /dev/null +++ b/doc/api/next_api_changes/2018-10-19-AL.rst @@ -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. diff --git a/lib/matplotlib/font_manager.py b/lib/matplotlib/font_manager.py index 620a41620d58..69e83ac41002 100644 --- a/lib/matplotlib/font_manager.py +++ b/lib/matplotlib/font_manager.py @@ -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 @@ -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 @@ -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] + return 0.95 * (abs(w1 - w2) / 1000) + 0.05 def score_size(self, size1, size2): """