Skip to content

Commit

Permalink
Resolve more mypy errors. (#808)
Browse files Browse the repository at this point in the history
* Add type annotation and correct errors.

* Format fixes by ruff

* Delete unused imports.

* Add/improve documentation and make other slight changes.

* Resolve `mypy` errors.

* Format fixes by ruff

* Respøve `mypy` errors related to `base.reference`.

* Format fixes by ruff

* Add annotation of `self` in `Interpolatable` protocol.

* Format fixes by ruff

---------

Co-authored-by: knutnergaard <knutnergaard@users.noreply.github.com>
Co-authored-by: Ben Kiel <ben@typefounding.com>
  • Loading branch information
3 people authored Dec 6, 2024
1 parent 56a51c0 commit 925f688
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 29 deletions.
12 changes: 9 additions & 3 deletions Lib/fontParts/base/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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: ...
10 changes: 6 additions & 4 deletions Lib/fontParts/base/bPoint.py
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -126,7 +126,7 @@ def _get_base_nextSegment(self) -> Optional[BaseSegment]:

# Contour

_contour: Optional[BaseContour] = None
_contour: Optional[Callable[[], BaseContour]] = None

contour = dynamicProperty(
"contour",
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion Lib/fontParts/base/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
13 changes: 8 additions & 5 deletions Lib/fontParts/base/component.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -68,7 +68,7 @@ def _reprContents(self) -> List[str]:

# Glyph

_glyph = None
_glyph: Optional[Callable[[], BaseGlyph]] = None

glyph: dynamicProperty = dynamicProperty(
"glyph",
Expand All @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions Lib/fontParts/base/contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
TYPE_CHECKING,
cast,
Any,
Callable,
Iterator,
List,
Optional,
Expand Down Expand Up @@ -105,7 +106,7 @@ def copyData(self, source: BaseContourType) -> None:

# Glyph

_glyph = None
_glyph: Optional[Callable[[], BaseGlyph]] = None

glyph: dynamicProperty = dynamicProperty(
"glyph",
Expand All @@ -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:
Expand Down
7 changes: 5 additions & 2 deletions Lib/fontParts/base/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
List,
Optional,
Tuple,
Union,
)
from collections.abc import MutableMapping

Expand Down Expand Up @@ -63,7 +64,7 @@ def _reprContents(self) -> List[str]:

# Font

_font = None
_font: Optional[Callable[[], BaseFont]] = None

font: dynamicProperty = dynamicProperty(
"font",
Expand All @@ -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:
Expand Down
7 changes: 5 additions & 2 deletions Lib/fontParts/base/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
List,
Optional,
Tuple,
Union,
)
import collections

Expand Down Expand Up @@ -673,7 +674,7 @@ def copyData(self, source: BaseLayer) -> None:

# Font

_font = None
_font: Optional[Callable[[], BaseFont]] = None

font: dynamicProperty = dynamicProperty(
"font",
Expand All @@ -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:
Expand Down
13 changes: 9 additions & 4 deletions Lib/fontParts/base/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Iterator,
List,
Optional,
Union,
)
from collections.abc import MutableMapping

Expand Down Expand Up @@ -64,7 +65,7 @@ def _reprContents(self) -> List[str]:

# Glyph

_glyph: Optional[BaseGlyph] = None
_glyph: Optional[Callable[[], BaseGlyph]] = None

glyph: dynamicProperty = dynamicProperty(
"glyph",
Expand All @@ -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:
Expand All @@ -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",
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 5 additions & 3 deletions Lib/fontParts/base/point.py
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down Expand Up @@ -66,7 +66,7 @@ def _reprContents(self) -> list[str]:

# Contour

_contour: Optional[BaseContour] = None
_contour: Optional[Callable[[], BaseContour]] = None

contour: dynamicProperty = dynamicProperty(
"contour",
Expand All @@ -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:
Expand Down
8 changes: 5 additions & 3 deletions Lib/fontParts/base/segment.py
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down Expand Up @@ -61,7 +61,7 @@ def _reprContents(self) -> List[str]:

# Contour

_contour: Optional[BaseContour] = None
_contour: Optional[Callable[[], BaseContour]] = None

contour: dynamicProperty = dynamicProperty(
"contour",
Expand All @@ -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:
Expand Down

0 comments on commit 925f688

Please sign in to comment.