|
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 |
5 | 4 |
|
6 | 5 | from networkx.classes.graph import Graph, _Node
|
7 | 6 | 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] |
11 | 8 |
|
12 | 9 | __all__ = ["from_agraph", "to_agraph", "write_dot", "read_dot", "graphviz_layout", "pygraphviz_layout", "view_pygraphviz"]
|
13 | 10 |
|
| 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 | + |
14 | 18 | @_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: ... |
18 | 26 | @_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 |
20 | 30 | def graphviz_layout(
|
21 | 31 | G: Graph[_Node], prog: str = "neato", root: str | None = None, args: str = ""
|
22 | 32 | ) -> 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]]: ... |
26 | 36 | def view_pygraphviz(
|
27 | 37 | 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 |
30 | 40 | # 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, |
32 | 42 | prog: str = "dot",
|
33 | 43 | args: str = "",
|
34 | 44 | suffix: str = "",
|
35 |
| - path: str | None = None, |
| 45 | + path: str | SupportsWrite[bytes] | _SupportsOpen[OpenBinaryModeUpdating, SupportsWrite[bytes]] | None = None, |
36 | 46 | show: bool = True,
|
37 |
| -): ... |
| 47 | +) -> tuple[str, AGraph]: ... |
0 commit comments