From 2429a187fc44441490c09c4049e05b3e78fe07a2 Mon Sep 17 00:00:00 2001 From: justvanrossum Date: Fri, 26 Feb 2021 20:18:37 +0100 Subject: [PATCH] Revert ABC changes to Pens, fixes #2198 --- Lib/fontTools/pens/basePen.py | 31 +------------------------------ Lib/fontTools/pens/pointPen.py | 23 +---------------------- Tests/pens/basePen_test.py | 23 ----------------------- Tests/pens/pointPen_test.py | 17 ----------------- 4 files changed, 2 insertions(+), 92 deletions(-) diff --git a/Lib/fontTools/pens/basePen.py b/Lib/fontTools/pens/basePen.py index 7202a9e45..34f89f8d2 100644 --- a/Lib/fontTools/pens/basePen.py +++ b/Lib/fontTools/pens/basePen.py @@ -36,7 +36,6 @@ Coordinates are usually expressed as (x, y) tuples, but generally any sequence of length 2 will do. """ -import abc from typing import Any, Tuple from fontTools.misc.loggingTools import LogMixin @@ -45,42 +44,18 @@ __all__ = ["AbstractPen", "NullPen", "BasePen", "decomposeSuperBezierSegment", "decomposeQuadraticSegment"] -class AbstractPen(abc.ABC): - @classmethod - def __subclasshook__(cls, subclass: Any) -> bool: - if cls is not AbstractPen: - return NotImplemented - return ( - hasattr(subclass, "moveTo") - and callable(subclass.moveTo) - and hasattr(subclass, "lineTo") - and callable(subclass.lineTo) - and hasattr(subclass, "curveTo") - and callable(subclass.curveTo) - and hasattr(subclass, "qCurveTo") - and callable(subclass.qCurveTo) - and hasattr(subclass, "closePath") - and callable(subclass.closePath) - and hasattr(subclass, "endPath") - and callable(subclass.endPath) - and hasattr(subclass, "addComponent") - and callable(subclass.addComponent) - or NotImplemented - ) +class AbstractPen: - @abc.abstractmethod def moveTo(self, pt: Tuple[float, float]) -> None: """Begin a new sub path, set the current point to 'pt'. You must end each sub path with a call to pen.closePath() or pen.endPath(). """ raise NotImplementedError - @abc.abstractmethod def lineTo(self, pt: Tuple[float, float]) -> None: """Draw a straight line from the current point to 'pt'.""" raise NotImplementedError - @abc.abstractmethod def curveTo(self, *points: Tuple[float, float]) -> None: """Draw a cubic bezier with an arbitrary number of control points. @@ -102,7 +77,6 @@ class AbstractPen(abc.ABC): """ raise NotImplementedError - @abc.abstractmethod def qCurveTo(self, *points: Tuple[float, float]) -> None: """Draw a whole string of quadratic curve segments. @@ -120,21 +94,18 @@ class AbstractPen(abc.ABC): """ raise NotImplementedError - @abc.abstractmethod def closePath(self) -> None: """Close the current sub path. You must call either pen.closePath() or pen.endPath() after each sub path. """ pass - @abc.abstractmethod def endPath(self) -> None: """End the current sub path, but don't close it. You must call either pen.closePath() or pen.endPath() after each sub path. """ pass - @abc.abstractmethod def addComponent( self, glyphName: str, diff --git a/Lib/fontTools/pens/pointPen.py b/Lib/fontTools/pens/pointPen.py index cd9e30ef3..92846d315 100644 --- a/Lib/fontTools/pens/pointPen.py +++ b/Lib/fontTools/pens/pointPen.py @@ -12,7 +12,6 @@ This allows the caller to provide more data for each point. For instance, whether or not a point is smooth, and its name. """ -import abc import math from typing import Any, List, Optional, Tuple @@ -28,36 +27,17 @@ __all__ = [ ] -class AbstractPointPen(abc.ABC): +class AbstractPointPen: """Baseclass for all PointPens.""" - @classmethod - def __subclasshook__(cls, subclass: Any) -> bool: - if cls is not AbstractPointPen: - return NotImplemented - return ( - hasattr(subclass, "beginPath") - and callable(subclass.beginPath) - and hasattr(subclass, "endPath") - and callable(subclass.endPath) - and hasattr(subclass, "addPoint") - and callable(subclass.addPoint) - and hasattr(subclass, "addComponent") - and callable(subclass.addComponent) - or NotImplemented - ) - - @abc.abstractmethod def beginPath(self, identifier: Optional[str] = None, **kwargs: Any) -> None: """Start a new sub path.""" raise NotImplementedError - @abc.abstractmethod def endPath(self) -> None: """End the current sub path.""" raise NotImplementedError - @abc.abstractmethod def addPoint( self, pt: Tuple[float, float], @@ -70,7 +50,6 @@ class AbstractPointPen(abc.ABC): """Add a point to the current sub path.""" raise NotImplementedError - @abc.abstractmethod def addComponent( self, baseGlyphName: str, diff --git a/Tests/pens/basePen_test.py b/Tests/pens/basePen_test.py index 059945c77..db57e80e8 100644 --- a/Tests/pens/basePen_test.py +++ b/Tests/pens/basePen_test.py @@ -5,29 +5,6 @@ from fontTools.misc.loggingTools import CapturingLogHandler import unittest -def test_subclasshook(): - class NullPen: - def moveTo(self, pt): - pass - def lineTo(self, pt): - pass - def curveTo(self, *points): - pass - def qCurveTo(self, *points): - pass - def closePath(self): - pass - def endPath(self): - pass - def addComponent(self, glyphName, transformation): - pass - - assert issubclass(NullPen, AbstractPen) - assert isinstance(NullPen(), AbstractPen) - assert not issubclass(NullPen, AbstractPointPen) - assert not isinstance(NullPen(), AbstractPointPen) - - class _TestPen(BasePen): def __init__(self): BasePen.__init__(self, glyphSet={}) diff --git a/Tests/pens/pointPen_test.py b/Tests/pens/pointPen_test.py index 80098ee6a..07261d039 100644 --- a/Tests/pens/pointPen_test.py +++ b/Tests/pens/pointPen_test.py @@ -5,23 +5,6 @@ from fontTools.pens.pointPen import AbstractPointPen, PointToSegmentPen, \ SegmentToPointPen, GuessSmoothPointPen, ReverseContourPointPen -def test_subclasshook(): - class NullPen: - def beginPath(self, identifier, **kwargs) -> None: - pass - def endPath(self) -> None: - pass - def addPoint(self, pt, segmentType, smooth, name, identifier, **kwargs) -> None: - pass - def addComponent(self, baseGlyphName, transformation, identifier, **kwargs) -> None: - pass - - assert issubclass(NullPen, AbstractPointPen) - assert isinstance(NullPen(), AbstractPointPen) - assert not issubclass(NullPen, AbstractPen) - assert not isinstance(NullPen(), AbstractPen) - - class _TestSegmentPen(AbstractPen): def __init__(self):