From 5a53d1d4ad63b94c05b51c3ddf4465b459d3e1a7 Mon Sep 17 00:00:00 2001 From: Nikolaus Waxweiler Date: Tue, 11 Feb 2020 13:53:25 +0000 Subject: [PATCH] models: Use new exceptions where input is checked --- Lib/fontTools/varLib/models.py | 13 ++++++++++--- Tests/varLib/models_test.py | 4 ++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Lib/fontTools/varLib/models.py b/Lib/fontTools/varLib/models.py index 2ffe33e2a..d6837ee62 100644 --- a/Lib/fontTools/varLib/models.py +++ b/Lib/fontTools/varLib/models.py @@ -5,6 +5,8 @@ __all__ = ['nonNone', 'allNone', 'allEqual', 'allEqualTo', 'subList', 'supportScalar', 'VariationModel'] +from .errors import VariationModelError + def nonNone(lst): return [l for l in lst if l is not None] @@ -43,7 +45,11 @@ def normalizeValue(v, triple): 0.5 """ lower, default, upper = triple - assert lower <= default <= upper, "invalid axis values: %3.3f, %3.3f %3.3f"%(lower, default, upper) + if not (lower <= default <= upper): + raise ValueError( + f"Invalid axis values, must be minimum, default, maximum: " + f"{lower:3.3f}, {default:3.3f}, {upper:3.3f}" + ) v = max(min(v, upper), lower) if v == default: v = 0. @@ -192,7 +198,7 @@ class VariationModel(object): def __init__(self, locations, axisOrder=None): if len(set(tuple(sorted(l.items())) for l in locations)) != len(locations): - raise ValueError("locations must be unique") + raise VariationModelError("Locations must be unique.") self.origLocations = locations self.axisOrder = axisOrder if axisOrder is not None else [] @@ -220,7 +226,8 @@ class VariationModel(object): @staticmethod def getMasterLocationsSortKeyFunc(locations, axisOrder=[]): - assert {} in locations, "Base master not found." + if {} not in locations: + raise VariationModelError("Base master not found.") axisPoints = {} for loc in locations: if len(loc) != 1: diff --git a/Tests/varLib/models_test.py b/Tests/varLib/models_test.py index 56f7104a2..1027f29f6 100644 --- a/Tests/varLib/models_test.py +++ b/Tests/varLib/models_test.py @@ -1,6 +1,6 @@ from fontTools.misc.py23 import * from fontTools.varLib.models import ( - normalizeLocation, supportScalar, VariationModel) + normalizeLocation, supportScalar, VariationModel, VariationModelError) import pytest @@ -145,7 +145,7 @@ class VariationModelTest(object): assert model.deltaWeights == deltaWeights def test_init_duplicate_locations(self): - with pytest.raises(ValueError, match="locations must be unique"): + with pytest.raises(VariationModelError, match="Locations must be unique."): VariationModel( [ {"foo": 0.0, "bar": 0.0},