Skip to content

Commit

Permalink
Merge pull request #118 from fonttools/some-typing-fixes
Browse files Browse the repository at this point in the history
Some typing fixes
  • Loading branch information
madig authored Dec 20, 2020
2 parents bf8dd5d + 35e37c8 commit a7b9a50
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 16 deletions.
10 changes: 5 additions & 5 deletions src/ufoLib2/objects/contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ def __iter__(self) -> Iterator[Point]:
def __len__(self) -> int:
return len(self.points)

def insert(self, index: int, point: Point) -> None:
"""Insert Point object ``point`` into the contour at ``index``."""
if not isinstance(point, Point):
raise TypeError(f"Expected Point, found {type(point).__name__}.")
self.points.insert(index, point)
def insert(self, index: int, value: Point) -> None:
"""Insert Point object ``value`` into the contour at ``index``."""
if not isinstance(value, Point):
raise TypeError(f"Expected Point, found {type(value).__name__}.")
self.points.insert(index, value)

# TODO: rotate method?

Expand Down
3 changes: 2 additions & 1 deletion src/ufoLib2/objects/font.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import attr
import fs
import fs.base
import fs.tempfs
from fontTools.ufoLib import UFOFileStructure, UFOReader, UFOWriter

Expand Down Expand Up @@ -306,7 +307,7 @@ def __ne__(self, other: object) -> bool:
return not result

@property
def reader(self) -> UFOReader:
def reader(self) -> Optional[UFOReader]:
"""Returns the underlying :class:`fontTools.ufoLib.UFOReader`."""
return self._reader

Expand Down
4 changes: 3 additions & 1 deletion src/ufoLib2/objects/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ def __bool__(self) -> bool:
"yOffset",
)
_valid_keys_: Tuple[str, str, str, str, str, str, str, str] = (
("fileName",) + _transformation_keys_ + ("color",)
"fileName",
*_transformation_keys_,
"color",
)

def __getitem__(self, key: str) -> Any:
Expand Down
32 changes: 24 additions & 8 deletions src/ufoLib2/objects/layer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
from typing import Any, Dict, Iterator, KeysView, Optional, Sequence, Set, Type, Union
from typing import (
Any,
Dict,
Iterator,
KeysView,
Optional,
Sequence,
Set,
Union,
overload,
)

import attr
from fontTools.ufoLib.glifLib import GlyphSet
Expand Down Expand Up @@ -185,24 +195,30 @@ def keys(self) -> KeysView[str]:
"""Returns a list of glyph names."""
return self._glyphs.keys()

def pop(
self, name: str, default: Union[Type[KeyError], T] = KeyError
) -> Union[Glyph, T]:
@overload
def pop(self, key: str) -> Glyph:
...

@overload
def pop(self, key: str, default: Union[Glyph, T] = ...) -> Union[Glyph, T]:
...

def pop(self, key: str, default: Union[Glyph, T] = KeyError) -> Union[Glyph, T]: # type: ignore
"""Remove and return glyph from layer.
Args:
name: The name of the glyph.
key: The name of the glyph.
default: What to return if there is no glyph with the given name.
"""
# XXX: make `default` a None instead of KeyError?
# NOTE: We can't defer to self._glyphs.pop because we must load glyphs
try:
glyph = self[name]
glyph = self[key]
except KeyError:
if default is KeyError:
raise
glyph = default # type: ignore
else:
del self[name]
del self[key]
return glyph

@property
Expand Down
1 change: 1 addition & 0 deletions src/ufoLib2/objects/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def _object_lib(parent_lib: Dict[str, Any], object: HasIdentifier) -> Dict[str,
# of the time.
object.identifier = str(uuid.uuid4())

object_libs: Dict[str, Any]
if "public.objectLibs" not in parent_lib:
object_libs = parent_lib["public.objectLibs"] = {}
else:
Expand Down
2 changes: 1 addition & 1 deletion src/ufoLib2/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
T = TypeVar("T")
"""Generic variable for mypy for trivial generic function signatures."""

PathLike = Union[str, bytes, os.PathLike]
PathLike = Union[str, bytes, "os.PathLike[str]", "os.PathLike[bytes]"]
"""Represents a path in various possible forms."""


Expand Down

0 comments on commit a7b9a50

Please sign in to comment.