models: Use new exceptions where input is checked

This commit is contained in:
Nikolaus Waxweiler 2020-02-11 13:53:25 +00:00 committed by Nikolaus Waxweiler
parent c5c30588b5
commit 5a53d1d4ad
2 changed files with 12 additions and 5 deletions

View File

@ -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:

View File

@ -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},