Skip to content

Commit

Permalink
Make ABC typings work in Python 3.7
Browse files Browse the repository at this point in the history
  • Loading branch information
madig committed Nov 9, 2021
1 parent dd881a0 commit e10ef17
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
10 changes: 8 additions & 2 deletions src/ufoLib2/objects/contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import warnings
from collections.abc import MutableSequence
from typing import Iterable, Iterator, overload
from typing import TYPE_CHECKING, Iterable, Iterator, overload

from attr import define, field
from fontTools.pens.basePen import AbstractPen
Expand All @@ -12,9 +12,15 @@
from ufoLib2.objects.point import Point
from ufoLib2.typing import GlyphSet

# For Python 3.7 compatibility.
if TYPE_CHECKING:
ContourMapping = MutableSequence[Point]
else:
ContourMapping = MutableSequence


@define
class Contour(MutableSequence[Point]):
class Contour(ContourMapping):
"""Represents a contour as a list of points.
Behavior:
Expand Down
10 changes: 8 additions & 2 deletions src/ufoLib2/objects/image.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
from __future__ import annotations

from collections.abc import Mapping
from typing import Any, Iterator
from typing import TYPE_CHECKING, Any, Iterator

from attr import define, field
from fontTools.misc.transform import Identity, Transform

from .misc import _convert_transform

# For Python 3.7 compatibility.
if TYPE_CHECKING:
ImageMapping = Mapping[str, Any]
else:
ImageMapping = Mapping


@define
class Image(Mapping[str, Any]):
class Image(ImageMapping):
"""Represents a background image reference.
See http://unifiedfontobject.org/versions/ufo3/images/ and
Expand Down
19 changes: 16 additions & 3 deletions src/ufoLib2/objects/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from abc import abstractmethod
from collections.abc import Mapping, MutableMapping
from copy import deepcopy
from typing import Any, Iterator, NamedTuple, Sequence, TypeVar, cast
from typing import TYPE_CHECKING, Any, Iterator, NamedTuple, Sequence, TypeVar, cast

import attr
from attr import define, field
Expand Down Expand Up @@ -115,9 +115,15 @@ class Placeholder:
# Create a generic variable for mypy that can be 'DataStore' or any subclass.
Tds = TypeVar("Tds", bound="DataStore")

# For Python 3.7 compatibility.
if TYPE_CHECKING:
DataStoreMapping = MutableMapping[str, bytes]
else:
DataStoreMapping = MutableMapping


@define
class DataStore(MutableMapping[str, bytes]):
class DataStore(DataStoreMapping):
"""Represents the base class for ImageSet and DataSet.
Both behave like a dictionary that loads its "values" lazily by default and only
Expand Down Expand Up @@ -267,7 +273,14 @@ def fileNames(self) -> list[str]:
return list(self._data.keys())


class AttrDictMixin(Mapping[str, Any]):
# For Python 3.7 compatibility.
if TYPE_CHECKING:
AttrDictMixinMapping = Mapping[str, Any]
else:
AttrDictMixinMapping = Mapping


class AttrDictMixin(AttrDictMixinMapping):
"""Read attribute values using mapping interface.
For use with Anchors and Guidelines classes, where client code
Expand Down

0 comments on commit e10ef17

Please sign in to comment.