From c00bc6fecf87a5c547bea6a076aebeefff14e91f Mon Sep 17 00:00:00 2001 From: Harry Dalton Date: Fri, 3 Jan 2025 12:49:28 +0000 Subject: [PATCH] Explicitly type arguments that are more liberal than their default value This avoids pitfalls from type checkers having to work with limited information, e.g., a type checker may implicitly assume that only integers are expected where a default value is 1, whereas in reality any float is accepted. --- Lib/fontTools/misc/transform.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Lib/fontTools/misc/transform.py b/Lib/fontTools/misc/transform.py index 2f4a216fa..3df50f219 100644 --- a/Lib/fontTools/misc/transform.py +++ b/Lib/fontTools/misc/transform.py @@ -52,6 +52,8 @@ translate, rotation, scale, skew, and transformation-center components. >>> """ +from __future__ import annotations + import math from typing import NamedTuple from dataclasses import dataclass @@ -214,7 +216,7 @@ class Transform(NamedTuple): xx, xy, yx, yy = self[:4] return [(xx * dx + yx * dy, xy * dx + yy * dy) for dx, dy in vectors] - def translate(self, x=0, y=0): + def translate(self, x: float = 0, y: float = 0): """Return a new transformation, translated (offset) by x, y. :Example: @@ -225,7 +227,7 @@ class Transform(NamedTuple): """ return self.transform((1, 0, 0, 1, x, y)) - def scale(self, x=1, y=None): + def scale(self, x: float = 1, y: float | None = None): """Return a new transformation, scaled by x, y. The 'y' argument may be None, which implies to use the x value for y as well. @@ -257,7 +259,7 @@ class Transform(NamedTuple): s = _normSinCos(math.sin(angle)) return self.transform((c, s, -s, c, 0, 0)) - def skew(self, x=0, y=0): + def skew(self, x: float = 0, y: float = 0): """Return a new transformation, skewed by x and y. :Example: @@ -381,7 +383,7 @@ class Transform(NamedTuple): Identity = Transform() -def Offset(x=0, y=0): +def Offset(x: float = 0, y: float = 0): """Return the identity transformation offset by x, y. :Example: @@ -392,7 +394,7 @@ def Offset(x=0, y=0): return Transform(1, 0, 0, 1, x, y) -def Scale(x, y=None): +def Scale(x: float, y: float | None = None): """Return the identity transformation scaled by x, y. The 'y' argument may be None, which implies to use the x value for y as well.