Skip to content

Commit b88f4c1

Browse files
authored
networkx: improve the nx_agraph module (#14554)
1 parent 5e40362 commit b88f4c1

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed
Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,47 @@
1-
from _typeshed import Incomplete
2-
from collections.abc import Callable, Hashable
3-
from io import TextIOBase
4-
from typing_extensions import TypeAlias
1+
from _typeshed import OpenBinaryModeUpdating, OpenTextModeReading, OpenTextModeWriting, SupportsWrite
2+
from collections.abc import Callable
3+
from typing import IO, Any, Protocol, TypeVar, type_check_only
54

65
from networkx.classes.graph import Graph, _Node
76
from networkx.utils.backends import _dispatchable
8-
9-
# from pygraphviz.agraph import AGraph as _AGraph
10-
_AGraph: TypeAlias = Incomplete
7+
from pygraphviz.agraph import AGraph # type: ignore[import-not-found] # pyright: ignore[reportMissingImports]
118

129
__all__ = ["from_agraph", "to_agraph", "write_dot", "read_dot", "graphviz_layout", "pygraphviz_layout", "view_pygraphviz"]
1310

11+
_ModeT_contra = TypeVar("_ModeT_contra", bound=str, contravariant=True)
12+
_FileT_co = TypeVar("_FileT_co", covariant=True)
13+
14+
@type_check_only
15+
class _SupportsOpen(Protocol[_ModeT_contra, _FileT_co]):
16+
def open(self, *, mode: _ModeT_contra) -> _FileT_co: ...
17+
1418
@_dispatchable
15-
def from_agraph(A, create_using=None) -> Graph[Incomplete]: ...
16-
def to_agraph(N: Graph[Hashable]) -> _AGraph: ...
17-
def write_dot(G: Graph[Hashable], path: str | TextIOBase) -> None: ...
19+
def from_agraph(
20+
A: AGraph, create_using: Graph[_Node] | type[Graph[_Node]] | None = None
21+
) -> Graph[_Node]: ... # type of node cannot be known statically
22+
def to_agraph(N: Graph[_Node]) -> AGraph: ...
23+
def write_dot(
24+
G: Graph[_Node], path: str | IO[str] | IO[bytes] | _SupportsOpen[OpenTextModeWriting, IO[str] | IO[bytes]]
25+
) -> None: ...
1826
@_dispatchable
19-
def read_dot(path: str | TextIOBase) -> Graph[Incomplete]: ...
27+
def read_dot(
28+
path: str | IO[str] | IO[bytes] | _SupportsOpen[OpenTextModeReading, IO[str] | IO[bytes]],
29+
) -> Graph[Any]: ... # type of node cannot be known statically
2030
def graphviz_layout(
2131
G: Graph[_Node], prog: str = "neato", root: str | None = None, args: str = ""
2232
) -> dict[_Node, tuple[float, float]]: ...
23-
24-
pygraphviz_layout = graphviz_layout
25-
33+
def pygraphviz_layout(
34+
G: Graph[_Node], prog: str = "neato", root: str | None = None, args: str = ""
35+
) -> dict[_Node, tuple[float, float]]: ...
2636
def view_pygraphviz(
2737
G: Graph[_Node],
28-
# From implementation looks like Callable could return object since it's always immediatly stringified
29-
# But judging by documentation this seems like an extra runtime safty thing and not intended
38+
# From implementation looks like Callable could return object since it's always immediately stringified
39+
# But judging by documentation this seems like an extra runtime safety thing and not intended
3040
# Leaving as str unless anyone reports a valid use-case
31-
edgelabel: str | Callable[[_Node], str] | None = None,
41+
edgelabel: str | Callable[[dict[str, Any]], str] | None = None,
3242
prog: str = "dot",
3343
args: str = "",
3444
suffix: str = "",
35-
path: str | None = None,
45+
path: str | SupportsWrite[bytes] | _SupportsOpen[OpenBinaryModeUpdating, SupportsWrite[bytes]] | None = None,
3646
show: bool = True,
37-
): ...
47+
) -> tuple[str, AGraph]: ...

0 commit comments

Comments
 (0)