Skip to content

Commit

Permalink
Use a Protocol to help typing of optional pygraphviz Graph type.
Browse files Browse the repository at this point in the history
  • Loading branch information
byllyfish committed Jan 28, 2024
1 parent 8851e8f commit bca24ef
Showing 1 changed file with 33 additions and 12 deletions.
45 changes: 33 additions & 12 deletions finsy/test/demonet.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,37 @@
from ipaddress import IPv4Network, IPv6Network
from pathlib import Path
from types import TracebackType
from typing import Any, ClassVar, Sequence
from typing import Any, ClassVar, Protocol, Sequence

from shellous import Command, Prompt, sh

from finsy import MACAddress

try:
import pygraphviz as pgv # type: ignore
except ImportError:
pgv = None

class GraphAPI(Protocol):
"""Protocol implemented by pygraphviz Graph.
pygraphviz is an optional dependency; this Protocol facilitates typing.
"""

def __init__(self, **attr: Any):
...

def add_node(self, n: Any, **attr: Any) -> None:
...

def add_edge(self, u: Any, v: Any, **attr: Any) -> None:
...

def layout(self) -> None:
...

def draw(self, path: str | None) -> bytes | None:
...

def to_string(self) -> str:
...


# pyright: reportUnknownMemberType=false

Expand Down Expand Up @@ -243,14 +264,14 @@ def _json_default(obj: object):
f"DemoNet can't serialize {type(obj).__name__!r}: {obj!r}"
) from ex

def to_graph(self) -> "pgv.AGraph":
def to_graph(self) -> GraphAPI:
"Create a pygraphviz Graph of the network."
if pgv is None:
raise RuntimeError("ERROR: pygraphviz is not installed.")
try:
import pygraphviz as pgv # type: ignore
except ImportError:
raise RuntimeError("ERROR: pygraphviz is not installed.") from None

graph = pgv.AGraph(
**_PyGraphStyle.graph, # pyright: ignore[reportArgumentType]
)
graph = pgv.AGraph(**_PyGraphStyle.graph)

for item in self.items:
match item:
Expand Down Expand Up @@ -611,7 +632,7 @@ def _get_local_ipv4_address():
class _PyGraphStyle:
"Style defaults for different graphviz elements."

graph: ClassVar[dict[str, str]] = dict(
graph: ClassVar[dict[str, Any]] = dict(
bgcolor="lightblue",
margin="0",
pad="0.25",
Expand Down

0 comments on commit bca24ef

Please sign in to comment.