Skip to content

Commit

Permalink
Corrected annotation (#776)
Browse files Browse the repository at this point in the history
* Solve mypy errors.

* Format fixes by ruff

* Remove guideline from appenGuideline doctoring

---------

Co-authored-by: knutnergaard <knutnergaard@users.noreply.github.com>
Co-authored-by: Ben Kiel <ben@benkiel.com>
  • Loading branch information
3 people authored Nov 18, 2024
1 parent 5bd14a2 commit bcf1da5
Showing 1 changed file with 37 additions and 26 deletions.
63 changes: 37 additions & 26 deletions Lib/fontParts/base/font.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
from typing import TYPE_CHECKING, Any, Generic, List, Optional, Tuple, Type, Union

from fontTools import ufoLib
from fontParts.base.errors import FontPartsError
from fontParts.base.base import dynamicProperty, InterpolationMixin
from fontParts.base.layer import _BaseGlyphVendor
Expand All @@ -12,6 +13,7 @@
from fontParts.base.annotations import (
CharacterMappingType,
CollectionType,
IntFloatType,
QuadrupleCollectionType,
PairCollectionType,
TransformationType,
Expand Down Expand Up @@ -1566,7 +1568,7 @@ def __setitem__(self, name: str, glyph: BaseGlyph) -> BaseGlyph:
""",
)

def _get_base_glyphOrder(self) -> Tuple[str]:
def _get_base_glyphOrder(self) -> Tuple[str, ...]:
value = self._get_glyphOrder()
value = normalizers.normalizeGlyphOrder(value)
return value
Expand All @@ -1575,7 +1577,7 @@ def _set_base_glyphOrder(self, value: CollectionType[str]) -> None:
value = normalizers.normalizeGlyphOrder(value)
self._set_glyphOrder(value)

def _get_glyphOrder(self) -> Tuple[str]:
def _get_glyphOrder(self) -> Tuple[str, ...]:
r"""Get the order of the glyphs in the native font.
This is the environment implementation of the
Expand Down Expand Up @@ -1745,10 +1747,10 @@ def _lenGuidelines(self, **kwargs: Any) -> int:
self.raiseNotImplementedError()

def _getitem__guidelines(self, index: int) -> BaseGuideline:
index = normalizers.normalizeIndex(index)
if index >= self._len__guidelines():
raise ValueError(f"No guideline located at index {index}.")
guideline = self._getGuideline(index)
normalizedIndex = normalizers.normalizeIndex(index)
if normalizedIndex is None or normalizedIndex >= self._len__guidelines():
raise ValueError(f"No guideline located at index {normalizedIndex}.")
guideline = self._getGuideline(normalizedIndex)
self._setFontInGuideline(guideline)
return guideline

Expand Down Expand Up @@ -1778,7 +1780,7 @@ def _getGuidelineIndex(self, guideline: BaseGuideline) -> int:
def appendGuideline(
self,
position: Optional[PairCollectionType[IntFloatType]] = None,
angle: Optional[float] = None,
angle: Optional[IntFloatType] = None,
name: Optional[str] = None,
color: Optional[QuadrupleCollectionType[IntFloatType]] = None,
guideline: Optional[BaseGuideline] = None,
Expand Down Expand Up @@ -1829,8 +1831,10 @@ def appendGuideline(
)
if normalizedGuideline.identifier not in existing:
identifier = normalizedGuideline.identifier
position = normalizers.normalizeCoordinateTuple(position)
angle = normalizers.normalizeRotationAngle(angle)
if position is not None:
position = normalizers.normalizeCoordinateTuple(position)
if angle is not None:
angle = normalizers.normalizeRotationAngle(angle)
if name is not None:
name = normalizers.normalizeGuidelineName(name)
if color is not None:
Expand All @@ -1848,7 +1852,6 @@ def _appendGuideline(
angle: Optional[float],
name: Optional[str],
color: Optional[QuadrupleCollectionType[IntFloatType]],
guideline: Optional[BaseGuideline],
**kwargs,
) -> BaseGuideline:
r"""Append a new guideline to the native font.
Expand All @@ -1861,9 +1864,6 @@ def _appendGuideline(
:param angle: The angle for the guideline as a :class:`float`.
:param name: The name for the guideline as a :class:`str`.
:param color: The color for the guideline as a :ref:`type-color`.
:param guideline: The :class:`BaseGuideline` subclass instance from
which to copy values. If `position`, `angle`, `name`, or `color`
are specified, those values will be used instead.
:param \**kwargs: Additional keyword arguments.
:return: The newly appended instance of
the :class:`BaseGuideline` subclass.
Expand Down Expand Up @@ -1892,10 +1892,13 @@ def removeGuideline(self, guideline: Union[int, BaseGuideline]) -> None:
index = guideline
else:
index = self._getGuidelineIndex(guideline)
index = normalizers.normalizeIndex(index)
if index >= self._len__guidelines():
raise ValueError(f"No guideline located at index {index}.")
self._removeGuideline(index)
normalizedIndex = normalizers.normalizeIndex(index)
# Avoid mypy conflict with normalizeIndex -> Optional[int]
if normalizedIndex is None:
return
if normalizedIndex >= self._len__guidelines():
raise ValueError(f"No guideline located at index {normalizedIndex}.")
self._removeGuideline(normalizedIndex)

def _removeGuideline(self, index: int, **kwargs: Any) -> None:
"""Remove the guideline at the specified index.
Expand Down Expand Up @@ -2366,8 +2369,8 @@ def _set_selectedLayerNames(self, value: List[str]) -> None:
"base_selectedGuidelines",
"""Get or set the selected guidelines in the font.
:param value: The :class:`list` of :class:`BaseGuideline` instances
to select.
:param value: The :class:`list` of either :class:`BaseGuideline` instances
to select or their corresponding indexes.
:return: A :class:`tuple` of currently selected :class:`BaseGuideline`
instances.
Expand Down Expand Up @@ -2411,17 +2414,25 @@ def _get_selectedGuidelines(self) -> Tuple[BaseGuideline, ...]:
"""
return self._getSelectedSubObjects(self.guidelines)

def _set_base_selectedGuidelines(self, value: List[BaseGuideline]) -> None:
def _set_base_selectedGuidelines(
self, value: List[Union[BaseGuideline, int]]
) -> None:
normalized = []
for i in value:
if isinstance(i, int):
i = normalizers.normalizeIndex(i)
for guideline in value:
normalizedGuideline: Union[BaseGuideline, int]
if isinstance(guideline, int):
normalizedIndex = normalizers.normalizeIndex(guideline)
# Avoid mypy conflict with normalizeIndex -> Optional[int]
if normalizedIndex is None:
continue
normalizedGuideline = normalizedIndex
else:
i = normalizers.normalizeGuideline(i)
normalized.append(i)
normalizedGuideline = normalizers.normalizeGuideline(guideline)

normalized.append(normalizedGuideline)
self._set_selectedGuidelines(normalized)

def _set_selectedGuidelines(self, value: List[BaseGuideline]) -> None:
def _set_selectedGuidelines(self, value: List[Union[BaseGuideline, int]]) -> None:
"""Set the selected guidelines in the native font.
This is the environment implementation of
Expand Down

0 comments on commit bcf1da5

Please sign in to comment.