From b30b8836dc6e3d8e42ae6cd537021f36e16e4dfc Mon Sep 17 00:00:00 2001 From: Knut Nergaard Date: Thu, 5 Dec 2024 23:36:44 +0100 Subject: [PATCH] =?UTF-8?q?Resp=C3=B8ve=20`mypy`=20errors=20related=20to?= =?UTF-8?q?=20`base.reference`.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Lib/fontParts/base/bPoint.py | 8 ++++---- Lib/fontParts/base/base.py | 2 +- Lib/fontParts/base/component.py | 11 ++++++----- Lib/fontParts/base/contour.py | 5 +++-- Lib/fontParts/base/groups.py | 5 +++-- Lib/fontParts/base/layer.py | 5 +++-- Lib/fontParts/base/lib.py | 9 +++++---- Lib/fontParts/base/point.py | 6 +++--- Lib/fontParts/base/segment.py | 6 +++--- 9 files changed, 31 insertions(+), 26 deletions(-) diff --git a/Lib/fontParts/base/bPoint.py b/Lib/fontParts/base/bPoint.py index 0fa173e5..20e05005 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,7 @@ 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 763b0604..0e5f791b 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 @@ -67,7 +67,7 @@ def _reprContents(self) -> List[str]: # Glyph - _glyph = None + _glyph: Optional[Callable[[], BaseGlyph]] = None glyph: dynamicProperty = dynamicProperty( "glyph", @@ -92,7 +92,7 @@ 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: @@ -431,9 +431,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..2533baa5 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,7 @@ 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..798f7dca 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,7 @@ 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 a4059e27..ad60e970 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,7 @@ 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..69727566 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,7 @@ 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 +103,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 +132,7 @@ 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..5724ca1b 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,7 @@ 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..5a12202b 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,7 @@ 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: