Skip to content

Commit 61d60a0

Browse files
authored
Finish adding type annotations (#85)
* Finish adding type annotations * exclude tests * clarify errors
1 parent 3e61055 commit 61d60a0

15 files changed

Lines changed: 49 additions & 30 deletions

File tree

graphviz2drawio/models/Arguments.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
class Arguments(ArgumentParser):
5-
def __init__(self, version) -> None:
5+
def __init__(self, version: str) -> None:
66
super().__init__(prog="graphviz2drawio")
77
self.add_argument("to_convert", metavar="file.dot", help="graphviz file")
88
output = self.add_mutually_exclusive_group()

graphviz2drawio/models/Errors.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from xml.etree.ElementTree import Element
2+
3+
14
class GdValueError(ValueError):
25
"""Base class for exceptions raised during conversion."""
36

@@ -8,22 +11,32 @@ def __init__(self, message: str) -> None:
811
class MissingTitleError(GdValueError):
912
"""Title missing from SVG element."""
1013

11-
def __init__(self, g) -> None:
12-
super().__init__("Title missing from SVG element.")
13-
self.g = g
14+
def __init__(self, g: Element) -> None:
15+
super().__init__(
16+
f"Title missing from SVG element: {g.tag} with attributes {g.attrib}.",
17+
)
18+
19+
20+
class MissingTextError(GdValueError):
21+
"""Title missing from SVG element."""
22+
23+
def __init__(self, g: Element) -> None:
24+
super().__init__(
25+
f"Text missing from SVG element: {g.tag} with attributes {g.attrib}.",
26+
)
1427

1528

1629
class InvalidBezierParameterError(GdValueError):
1730
"""Invalid Bezier parameter, must be 0 <= t <= 1."""
1831

19-
def __init__(self, t) -> None:
32+
def __init__(self, t: float) -> None:
2033
super().__init__(f"Invalid Bezier parameter (t={t}), must be 0 <= t <= 1.")
2134

2235

2336
class MissingIdentifiersError(GdValueError):
2437
"""Missing identifiers for a geometry."""
2538

26-
def __init__(self, sid, gid) -> None:
39+
def __init__(self, sid: str | None, gid: str | None) -> None:
2740
super().__init__(
2841
f"Missing identifiers for a geometry: sid(id): {sid}, gid(title): {gid}.",
2942
)

graphviz2drawio/models/Rect.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def to_dict_str(self) -> dict[str, str]:
3838
"height": str(self.height),
3939
}
4040

41-
def closest_point_along_perimeter(self, x: float, y: float):
41+
def closest_point_along_perimeter(self, x: float, y: float) -> tuple[float, float]:
4242
x = clamp(x, self.x, self.right)
4343
y = clamp(y, self.y, self.bottom)
4444

@@ -62,5 +62,5 @@ def relative_location_along_perimeter(self, point: complex) -> tuple[float, floa
6262
return self.x_ratio(p[0]), self.y_ratio(p[1])
6363

6464

65-
def clamp(value, min_v, max_v):
65+
def clamp(value: float, min_v: float, max_v: float) -> float:
6666
return max(min(value, max_v), min_v)

graphviz2drawio/models/SVG.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
NS_SVG = "{http://www.w3.org/2000/svg}"
44

55

6-
def svg_tag(tag):
6+
def svg_tag(tag: str) -> str:
77
return f"{NS_SVG}{tag}"
88

99

@@ -27,5 +27,5 @@ def get_text(g: Element) -> str | None:
2727
return None
2828

2929

30-
def is_tag(g, tag):
30+
def is_tag(g: Element, tag: str) -> bool:
3131
return g.tag == svg_tag(tag)

graphviz2drawio/mx/Curve.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def y(x: float) -> float:
6767
return y
6868

6969

70-
def _rotate_bezier(cb):
70+
def _rotate_bezier(cb: CubicBezier) -> CubicBezier:
7171
"""Reverse imaginary and real parts for all components."""
7272
return CubicBezier(
7373
complex(cb.start.imag, cb.start.real),

graphviz2drawio/mx/CurveFactory.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import cmath
2+
from typing import Any
23

34
from svg.path import CubicBezier, Path, QuadraticBezier, parse_path
45

@@ -54,5 +55,5 @@ def from_svg(self, svg_path: str) -> Curve:
5455
return Curve(start=start, end=end, is_bezier=is_bezier, points=points)
5556

5657

57-
def _is_cubic(p):
58+
def _is_cubic(p: Any) -> bool: # noqa: ANN401
5859
return isinstance(p, CubicBezier)

graphviz2drawio/mx/Edge.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,6 @@ def _get_arrow_shape_and_fill(
9797

9898
return shape, fill
9999

100-
def curve_start_end(self):
101-
if self.dir == DotAttr.BACK:
102-
return self.curve.end, self.curve.start
103-
return self.curve.start, self.curve.end
104-
105100
@property
106101
def key_for_label(self) -> str:
107102
return f"{self.gid}-{self.curve}-{self.dir}"

graphviz2drawio/mx/EdgeFactory.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from xml.etree.ElementTree import Element
2+
13
from graphviz2drawio.models import SVG
24

35
from ..models.CoordsTranslate import CoordsTranslate
@@ -13,7 +15,7 @@ def __init__(self, coords: CoordsTranslate, *, is_directed: bool) -> None:
1315
self.curve_factory = CurveFactory(coords)
1416
self.is_directed = is_directed
1517

16-
def from_svg(self, g) -> Edge:
18+
def from_svg(self, g: Element) -> Edge:
1719
title = SVG.get_title(g)
1820
if title is None:
1921
raise MissingTitleError(g)

graphviz2drawio/mx/GraphObj.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
class GraphObj:
55

6-
def __init__(self, sid, gid) -> None:
6+
def __init__(self, sid: str, gid: str) -> None:
77
self.sid = sid
88
self.gid = gid
99

10-
def enrich_from_graph(self, attrs) -> None:
10+
def enrich_from_graph(self, attrs: list) -> None:
1111
for k, v in attrs:
1212
if (
1313
k not in _whitelist_attrs

graphviz2drawio/mx/MxGraph.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from xml.etree.ElementTree import Element, SubElement, indent, tostring
33

44
from graphviz2drawio.models import DotAttr
5+
from graphviz2drawio.models.Rect import Rect
56
from graphviz2drawio.mx import MxConst
67
from graphviz2drawio.mx.Curve import Curve
78
from graphviz2drawio.mx.Edge import Edge
@@ -97,7 +98,7 @@ def add_node(self, node: Node) -> None:
9798
self.add_mx_geo(node_element, node.rect)
9899

99100
@staticmethod
100-
def add_mx_geo(element, rect=None) -> None:
101+
def add_mx_geo(element: Element, rect: Rect | None = None) -> None:
101102
if rect is None:
102103
SubElement(element, MxConst.GEO, attrib={"as": "geometry", "relative": "1"})
103104
else:

0 commit comments

Comments
 (0)