diff --git a/Lib/fontParts/base/annotations.py b/Lib/fontParts/base/annotations.py index 11908339..a02df1ee 100644 --- a/Lib/fontParts/base/annotations.py +++ b/Lib/fontParts/base/annotations.py @@ -54,8 +54,14 @@ class Interpolatable(Protocol): """Represent a protocol for interpolatable types.""" - def __add__(self, other: InterpolatableType) -> InterpolatableType: ... + def __add__( + self: InterpolatableType, other: InterpolatableType + ) -> InterpolatableType: ... - def __sub__(self, other: InterpolatableType) -> InterpolatableType: ... + def __sub__( + self: InterpolatableType, other: InterpolatableType + ) -> InterpolatableType: ... - def __mul__(self, other: TransformationType) -> InterpolatableType: ... + def __mul__( + self: InterpolatableType, other: TransformationType + ) -> InterpolatableType: ... diff --git a/Lib/fontParts/base/bPoint.py b/Lib/fontParts/base/bPoint.py index 0fa173e5..525a3448 100644 --- a/Lib/fontParts/base/bPoint.py +++ b/Lib/fontParts/base/bPoint.py @@ -1,5 +1,5 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Any, List, Optional +from typing import TYPE_CHECKING, Any, Callable, List, Optional, Union from fontTools.misc import transform from fontParts.base.base import ( @@ -59,7 +59,7 @@ def __eq__(self, other): # this class should not be used in hashable # collections since it is dynamically generated. - __hash__ = None + __hash__ = None # type: ignore[assignment] # ------- # Parents @@ -126,7 +126,7 @@ def _get_base_nextSegment(self) -> Optional[BaseSegment]: # Contour - _contour: Optional[BaseContour] = None + _contour: Optional[Callable[[], BaseContour]] = None contour = dynamicProperty( "contour", @@ -151,7 +151,9 @@ def _get_contour(self) -> Optional[BaseContour]: return None return self._contour() - def _set_contour(self, contour: Optional[BaseContour]) -> None: + def _set_contour( + self, contour: Optional[Union[BaseContour, Callable[[], BaseContour]]] + ) -> None: if self._contour is not None: raise AssertionError("contour for bPoint already set") if contour is not None: diff --git a/Lib/fontParts/base/base.py b/Lib/fontParts/base/base.py index a0b6da74..e355e166 100644 --- a/Lib/fontParts/base/base.py +++ b/Lib/fontParts/base/base.py @@ -1631,7 +1631,7 @@ def raiseNotImplementedError(self): pass -def reference(obj: Callable[[], Any]) -> Callable[[], Any]: +def reference(obj: Any) -> Callable[[], Any]: """ This code returns a simple function that returns the given object. This is a backwards compatibility function that is under review (see issue #749). diff --git a/Lib/fontParts/base/component.py b/Lib/fontParts/base/component.py index 14f584bf..f8f1d6d3 100644 --- a/Lib/fontParts/base/component.py +++ b/Lib/fontParts/base/component.py @@ -1,5 +1,5 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Any, List, Optional, Tuple +from typing import TYPE_CHECKING, Any, Callable, List, Optional, Tuple, Union from fontTools.misc import transform from fontTools.pens.pointInsidePen import PointInsidePen @@ -68,7 +68,7 @@ def _reprContents(self) -> List[str]: # Glyph - _glyph = None + _glyph: Optional[Callable[[], BaseGlyph]] = None glyph: dynamicProperty = dynamicProperty( "glyph", @@ -93,7 +93,9 @@ def _get_glyph(self) -> Optional[BaseGlyph]: return None return self._glyph() - def _set_glyph(self, glyph: Optional[BaseGlyph]) -> None: + def _set_glyph( + self, glyph: Optional[Union[BaseGlyph, Callable[[], BaseGlyph]]] + ) -> None: if self._glyph is not None: raise AssertionError("glyph for component already set") if glyph is not None: @@ -432,9 +434,10 @@ def _set_base_index(self, value: int) -> None: glyph = self.glyph if glyph is None: raise FontPartsError("The component does not belong to a glyph.") - value = normalizers.normalizeIndex(value) - if value is None: + index = normalizers.normalizeIndex(value) + if index is None: return + value = index componentCount = len(glyph.components) if value < 0: value = -(value % componentCount) diff --git a/Lib/fontParts/base/contour.py b/Lib/fontParts/base/contour.py index 041bea4a..1f11c503 100644 --- a/Lib/fontParts/base/contour.py +++ b/Lib/fontParts/base/contour.py @@ -3,6 +3,7 @@ TYPE_CHECKING, cast, Any, + Callable, Iterator, List, Optional, @@ -105,7 +106,7 @@ def copyData(self, source: BaseContourType) -> None: # Glyph - _glyph = None + _glyph: Optional[Callable[[], BaseGlyph]] = None glyph: dynamicProperty = dynamicProperty( "glyph", @@ -130,7 +131,9 @@ def _get_glyph(self) -> Optional[BaseGlyph]: return None return self._glyph() - def _set_glyph(self, glyph: Optional[BaseGlyph]) -> None: + def _set_glyph( + self, glyph: Optional[Union[BaseGlyph, Callable[[], BaseGlyph]]] + ) -> None: if self._glyph is not None: raise AssertionError("glyph for contour already set") if glyph is not None: diff --git a/Lib/fontParts/base/groups.py b/Lib/fontParts/base/groups.py index d35e7590..1ad554ff 100644 --- a/Lib/fontParts/base/groups.py +++ b/Lib/fontParts/base/groups.py @@ -7,6 +7,7 @@ List, Optional, Tuple, + Union, ) from collections.abc import MutableMapping @@ -63,7 +64,7 @@ def _reprContents(self) -> List[str]: # Font - _font = None + _font: Optional[Callable[[], BaseFont]] = None font: dynamicProperty = dynamicProperty( "font", @@ -89,7 +90,9 @@ def _get_font(self) -> Optional[BaseFont]: return None return self._font() - def _set_font(self, font: Optional[BaseFont]) -> None: + def _set_font( + self, font: Optional[Union[BaseFont, Callable[[], BaseFont]]] + ) -> None: if self._font is not None and self._font != font: raise AssertionError("font for groups already set and is not same as font") if font is not None: diff --git a/Lib/fontParts/base/layer.py b/Lib/fontParts/base/layer.py index 081e81eb..480bc146 100644 --- a/Lib/fontParts/base/layer.py +++ b/Lib/fontParts/base/layer.py @@ -9,6 +9,7 @@ List, Optional, Tuple, + Union, ) import collections @@ -673,7 +674,7 @@ def copyData(self, source: BaseLayer) -> None: # Font - _font = None + _font: Optional[Callable[[], BaseFont]] = None font: dynamicProperty = dynamicProperty( "font", @@ -698,7 +699,9 @@ def _get_font(self) -> Optional[BaseFont]: return None return self._font() - def _set_font(self, font: Optional[BaseFont]) -> None: + def _set_font( + self, font: Optional[Union[BaseFont, Callable[[], BaseFont]]] + ) -> None: if self._font is not None: raise AssertionError("font for layer already set") if font is not None: diff --git a/Lib/fontParts/base/lib.py b/Lib/fontParts/base/lib.py index 9f4efbb7..7f27652d 100644 --- a/Lib/fontParts/base/lib.py +++ b/Lib/fontParts/base/lib.py @@ -6,6 +6,7 @@ Iterator, List, Optional, + Union, ) from collections.abc import MutableMapping @@ -64,7 +65,7 @@ def _reprContents(self) -> List[str]: # Glyph - _glyph: Optional[BaseGlyph] = None + _glyph: Optional[Callable[[], BaseGlyph]] = None glyph: dynamicProperty = dynamicProperty( "glyph", @@ -91,7 +92,9 @@ def _get_glyph(self) -> Optional[BaseGlyph]: return None return self._glyph() - def _set_glyph(self, glyph: Optional[BaseGlyph]) -> None: + def _set_glyph( + self, glyph: Optional[Union[BaseGlyph, Callable[[], BaseGlyph]]] + ) -> None: if self._font is not None: raise AssertionError("font for lib already set") if self._glyph is not None and self._glyph() != glyph: @@ -102,7 +105,7 @@ def _set_glyph(self, glyph: Optional[BaseGlyph]) -> None: # Font - _font: Optional[BaseFont] = None + _font: Optional[Callable[[], BaseFont]] = None font: dynamicProperty = dynamicProperty( "font", @@ -131,7 +134,9 @@ def _get_font(self) -> Optional[BaseFont]: return self.glyph.font return None - def _set_font(self, font: Optional[BaseFont]) -> None: + def _set_font( + self, font: Optional[Union[BaseFont, Callable[[], BaseFont]]] + ) -> None: if self._font is not None and self._font() != font: raise AssertionError("font for lib already set and is not same as font") if self._glyph is not None: diff --git a/Lib/fontParts/base/point.py b/Lib/fontParts/base/point.py index b32a2558..e6e35699 100644 --- a/Lib/fontParts/base/point.py +++ b/Lib/fontParts/base/point.py @@ -1,5 +1,5 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Any, Optional, Union, Tuple +from typing import TYPE_CHECKING, Any, Callable, Optional, Union, Tuple from fontTools.misc import transform from fontParts.base.base import ( @@ -66,7 +66,7 @@ def _reprContents(self) -> list[str]: # Contour - _contour: Optional[BaseContour] = None + _contour: Optional[Callable[[], BaseContour]] = None contour: dynamicProperty = dynamicProperty( "contour", @@ -91,7 +91,9 @@ def _get_contour(self) -> Optional[BaseContour]: return None return self._contour() - def _set_contour(self, contour: Optional[BaseContour]) -> None: + def _set_contour( + self, contour: Optional[Union[BaseContour, Callable[[], BaseContour]]] + ) -> None: if self._contour is not None: raise AssertionError("contour for point already set") if contour is not None: diff --git a/Lib/fontParts/base/segment.py b/Lib/fontParts/base/segment.py index 5737c581..3cdbaaa7 100644 --- a/Lib/fontParts/base/segment.py +++ b/Lib/fontParts/base/segment.py @@ -1,5 +1,5 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Any, Generator, List, Optional, Tuple +from typing import TYPE_CHECKING, Any, Callable, Generator, List, Optional, Tuple, Union from fontParts.base.errors import FontPartsError from fontParts.base.base import ( @@ -61,7 +61,7 @@ def _reprContents(self) -> List[str]: # Contour - _contour: Optional[BaseContour] = None + _contour: Optional[Callable[[], BaseContour]] = None contour: dynamicProperty = dynamicProperty( "contour", @@ -86,7 +86,9 @@ def _get_contour(self) -> Optional[BaseContour]: return None return self._contour() - def _set_contour(self, contour: Optional[BaseContour]) -> None: + def _set_contour( + self, contour: Optional[Union[BaseContour, Callable[[], BaseContour]]] + ) -> None: if self._contour is not None: raise AssertionError("contour for segment already set") if contour is not None: