Skip to content

Commit

Permalink
Implement abstract members.
Browse files Browse the repository at this point in the history
  • Loading branch information
knutnergaard committed Dec 5, 2024
1 parent 466050c commit 0837023
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 22 deletions.
6 changes: 3 additions & 3 deletions Lib/fontParts/base/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def interpolate(
# ------------


class BaseObject:
class BaseObject(Generic[BaseObjectType]):
r"""Provide common base functionality to objects.
This class is intended to serve as a foundation for other classes, supplying
Expand Down Expand Up @@ -1399,13 +1399,13 @@ def _set_selected(self, value: bool) -> None:
# Sub-Objects
# -----------
@classmethod
def _getSelectedSubObjects(cls, subObjects: CollectionType[Any]) -> Tuple[Any]:
def _getSelectedSubObjects(cls, subObjects: Any) -> Tuple[Any]:
selected = tuple(obj for obj in subObjects if obj.selected)
return selected

@classmethod
def _setSelectedSubObjects(
cls, subObjects: CollectionType[Any], selected: CollectionType[Any]
cls, subObjects: Any, selected: Any
) -> None:
for obj in subObjects:
obj.selected = obj in selected
Expand Down
5 changes: 3 additions & 2 deletions Lib/fontParts/base/contour.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import annotations
from typing import TYPE_CHECKING, cast, Any, Iterator, List, Optional, Tuple, Union
from typing import (TYPE_CHECKING, cast, Any, Iterator, List, Optional, Tuple, TypeVar, Union)

from fontParts.base.errors import FontPartsError
from fontParts.base.base import (
Expand Down Expand Up @@ -32,6 +32,7 @@
from fontParts.base.layer import BaseLayer
from fontParts.base.font import BaseFont

BaseContourType = TypeVar("BaseContourType", bound="BaseContour")
PointCollectionType = CollectionType[PairCollectionType[IntFloatType]]


Expand Down Expand Up @@ -65,7 +66,7 @@ def _reprContents(self) -> List[str]:
contents += self.glyph._reprContents()
return contents

def copyData(self, source: BaseContour) -> None:
def copyData(self, source: BaseContourType) -> None:
"""Copy data from another contour instance.
This will copy the contents of the following attributes from `source`
Expand Down
30 changes: 15 additions & 15 deletions Lib/fontParts/base/font.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def _get_base_path(self) -> Optional[str]:
path = normalizers.normalizeFilePath(path)
return path

def _get_path(self, **kwargs: Any) -> Optional[str]:
def _get_path(self, **kwargs: Any) -> Optional[str]: # type: ignore[return]
r"""Get the path to the native font file.
This method is the environment implementation
Expand Down Expand Up @@ -574,7 +574,7 @@ def _get_base_info(self) -> BaseInfo:
info.font = self
return info

def _get_info(self) -> BaseInfo:
def _get_info(self) -> BaseInfo: # type: ignore[return]
"""Get the native font's info object.
This is the environment implementation of :attr:`BaseFont.info`.
Expand Down Expand Up @@ -613,7 +613,7 @@ def _get_base_groups(self) -> BaseGroups:
groups.font = self
return groups

def _get_groups(self) -> BaseGroups:
def _get_groups(self) -> BaseGroups: # type: ignore[return]
"""Get the native font's groups object.
This is the environment implementation
Expand Down Expand Up @@ -653,7 +653,7 @@ def _get_base_kerning(self) -> BaseKerning:
kerning.font = self
return kerning

def _get_kerning(self) -> BaseKerning:
def _get_kerning(self) -> BaseKerning: # type: ignore[return]
"""Get the native font's kerning object.
This is the environment implementation
Expand Down Expand Up @@ -752,7 +752,7 @@ def _get_base_features(self) -> BaseFeatures:
features.font = self
return features

def _get_features(self) -> BaseFeatures:
def _get_features(self) -> BaseFeatures: # type: ignore[return]
"""Get the native font's features object.
This is the environment implementation of
Expand Down Expand Up @@ -792,7 +792,7 @@ def _get_base_lib(self) -> BaseLib:
lib.font = self
return lib

def _get_lib(self) -> BaseLib:
def _get_lib(self) -> BaseLib: # type: ignore[return]
"""Get the native font's lib object.
This is the environment implementation of :attr:`BaseFont.lib`.
Expand Down Expand Up @@ -837,7 +837,7 @@ def _get_base_tempLib(self) -> BaseLib:
lib.font = self
return lib

def _get_tempLib(self) -> BaseLib:
def _get_tempLib(self) -> BaseLib: # type: ignore[return]
"""Get the native font's temporary lib object.
This is the environment implementation
Expand Down Expand Up @@ -883,7 +883,7 @@ def _get_base_layers(self) -> Tuple[BaseLayer, ...]:
self._setFontInLayer(layer)
return tuple(layers)

def _get_layers(self, **kwargs: Any) -> Tuple[BaseLayer, ...]:
def _get_layers(self, **kwargs: Any) -> Tuple[BaseLayer, ...]: # type: ignore[return]
r"""Get the native font's layer objects.
This is the environment implementation of
Expand Down Expand Up @@ -932,7 +932,7 @@ def _set_base_layerOrder(self, value: CollectionType[str]) -> None:
value = normalizers.normalizeLayerOrder(value, self)
self._set_layerOrder(value)

def _get_layerOrder(self, **kwargs: Any) -> Tuple[str, ...]:
def _get_layerOrder(self, **kwargs: Any) -> Tuple[str, ...]: # type: ignore[return]
r"""Get the order of the layers in the native font.
This is the environment implementation of the
Expand Down Expand Up @@ -1006,7 +1006,7 @@ def _set_base_defaultLayerName(self, value: str) -> None:
value = normalizers.normalizeDefaultLayerName(value, self)
self._set_defaultLayerName(value)

def _get_defaultLayerName(self) -> str:
def _get_defaultLayerName(self) -> str: # type: ignore[return]
"""Get the name of the native font's default layer.
This is the environment implementation of
Expand Down Expand Up @@ -1181,7 +1181,7 @@ def newLayer(
self._setFontInLayer(layer)
return layer

def _newLayer(
def _newLayer( # type: ignore[return]
self,
name: str,
color: Optional[QuadrupleCollectionType[IntFloatType]],
Expand Down Expand Up @@ -1577,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, ...]: # type: ignore[return]
r"""Get the order of the glyphs in the native font.
This is the environment implementation of the
Expand Down Expand Up @@ -1732,7 +1732,7 @@ def _get_guidelines(self) -> Tuple[BaseGuideline, ...]:
def _len__guidelines(self) -> int:
return self._lenGuidelines()

def _lenGuidelines(self, **kwargs: Any) -> int:
def _lenGuidelines(self, **kwargs: Any) -> int: # type: ignore[return]
r"""Return the number of font-level guidelines in the native font.
:param \**kwargs: Additional keyword arguments.
Expand All @@ -1754,7 +1754,7 @@ def _getitem__guidelines(self, index: int) -> BaseGuideline:
self._setFontInGuideline(guideline)
return guideline

def _getGuideline(self, index: int, **kwargs: Any) -> BaseGuideline:
def _getGuideline(self, index: int, **kwargs: Any) -> BaseGuideline: # type: ignore[return]
r"""Return the guideline at the given index.
:param index: The index of the guideline.
Expand Down Expand Up @@ -1846,7 +1846,7 @@ def appendGuideline(
newGuideline.font = self
return newGuideline

def _appendGuideline(
def _appendGuideline( # type: ignore[return]
self,
position: Optional[PairCollectionType[IntFloatType]],
angle: Optional[float],
Expand Down
45 changes: 43 additions & 2 deletions Lib/fontParts/base/layer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# pylint: disable=C0103, C0302, C0114, W0613
from __future__ import annotations
from typing import TYPE_CHECKING, Any, Callable, Iterator, List, Optional, Tuple
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, Any, Callable, Iterator, List, NoReturn, Optional, Tuple, TypeVar
import collections

from fontParts.base.base import (
Expand Down Expand Up @@ -29,7 +30,7 @@
from fontParts.base.lib import BaseLib


class _BaseGlyphVendor(BaseObject, SelectionMixin):
class _BaseGlyphVendor(BaseObject, SelectionMixin, ABC):
"""Provide common glyph interaction.
This class provides common glyph interaction code to the
Expand Down Expand Up @@ -573,6 +574,28 @@ def _set_selectedGlyphNames(self, value: CollectionType[str]) -> None:

has_key: Callable[[_BaseGlyphVendor, str], bool] = __contains__

# ----------------
# Abstract Members
# ----------------

defaultLayer: dynamicProperty = dynamicProperty("base_defaultLayer")

@abstractmethod
def _get_base_defaultLayer(self) -> BaseLayer:
pass

@abstractmethod
def _set_base_defaultLayer(self, layer: BaseLayer) -> None:
pass

@abstractmethod
def _get_defaultLayer(self) -> BaseLayer:
pass

@abstractmethod
def _set_defaultLayer(self, value: BaseLayer) -> None:
pass


class BaseLayer(_BaseGlyphVendor, InterpolationMixin, DeprecatedLayer, RemovedLayer):
"""Represent the basis for a layer object.
Expand Down Expand Up @@ -1224,3 +1247,21 @@ def _getCharacterMapping(self) -> CharacterMappingType:
for code in glyph.unicodes:
mapping[code].append(glyph.name)
return {k: tuple(v) for k, v in mapping.items()}

# --------------------------
# Abstract Members Overrides
# --------------------------

defaultLayer: dynamicProperty = dynamicProperty("base_defaultLayer")

def _get_base_defaultLayer(self) -> BaseLayer:
raise NotImplementedError("BaseLayer does not implement this method.")

def _set_base_defaultLayer(self, layer: BaseLayer) -> None:
raise NotImplementedError("BaseLayer does not implement this method.")

def _get_defaultLayer(self) -> BaseLayer:
raise NotImplementedError("BaseLayer does not implement this method.")

def _set_defaultLayer(self, value: BaseLayer) -> None:
raise NotImplementedError("BaseLayer does not implement this method.")

0 comments on commit 0837023

Please sign in to comment.