Skip to content

Commit 81639ac

Browse files
authored
networkx: complete the nx_latex module (#14581)
1 parent 706320c commit 81639ac

File tree

2 files changed

+64
-19
lines changed

2 files changed

+64
-19
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import networkx as nx
2+
3+
# Test covariant dict type for `pos` in nx_latex functions
4+
G: "nx.Graph[int]" = nx.Graph([(1, 2), (2, 3), (3, 4)])
5+
nx.to_latex_raw(G, pos=nx.spring_layout(G, seed=42)) # OK: dict[node, ndarray]
6+
pos1: dict[int, tuple[int, int]] = {1: (1, 2), 2: (3, 4), 3: (5, 6), 4: (7, 8)}
7+
nx.to_latex_raw(G, pos=pos1) # OK: dict[node, 2-tuple]
8+
pos2: dict[int, str] = {1: "(1, 2)", 2: "(3, 4)", 3: "(5, 6)", 4: "(7, 8)"}
9+
nx.to_latex_raw(G, pos=pos2) # OK: dict[node, str]
10+
pos3: dict[int, int] = {1: 1, 2: 3, 3: 5, 4: 7}
11+
nx.to_latex_raw(G, pos=pos3) # type: ignore # dict keys must be str or collection
Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,70 @@
1+
from _typeshed import StrPath, SupportsWrite
2+
from collections.abc import Collection
3+
from typing_extensions import TypeAlias, TypeVar
4+
5+
from networkx.classes.graph import Graph, _Node
6+
17
__all__ = ["to_latex_raw", "to_latex", "write_latex"]
28

9+
# runtime requires a dict but it doesn't mutate it, we use a bounded typevar as
10+
# a values type to make type checkers treat the dict covariantely
11+
_PosT = TypeVar("_PosT", bound=Collection[float] | str)
12+
_Pos: TypeAlias = str | dict[_Node, _PosT]
13+
314
def to_latex_raw(
4-
G,
5-
pos: str = "pos",
15+
G: Graph[_Node],
16+
pos: _Pos[_Node, _PosT] = "pos",
617
tikz_options: str = "",
718
default_node_options: str = "",
8-
node_options: str = "node_options",
9-
node_label: str = "label",
19+
node_options: str | dict[_Node, str] = "node_options",
20+
node_label: str | dict[_Node, str] = "label",
1021
default_edge_options: str = "",
11-
edge_options: str = "edge_options",
12-
edge_label: str = "label",
13-
edge_label_options: str = "edge_label_options",
14-
): ...
22+
edge_options: str | dict[tuple[_Node, _Node], str] = "edge_options",
23+
edge_label: str | dict[tuple[_Node, _Node], str] = "label",
24+
edge_label_options: str | dict[tuple[_Node, _Node], str] = "edge_label_options",
25+
) -> str: ...
1526
def to_latex(
16-
Gbunch,
17-
pos: str = "pos",
27+
Gbunch: Graph[_Node] | Collection[Graph[_Node]],
28+
pos: _Pos[_Node, _PosT] | Collection[_Pos[_Node, _PosT]] = "pos",
29+
tikz_options: str = "",
30+
default_node_options: str = "",
31+
node_options: str | dict[_Node, str] = "node_options",
32+
node_label: str | dict[_Node, str] = "node_label",
33+
default_edge_options: str = "",
34+
edge_options: str | dict[tuple[_Node, _Node], str] = "edge_options",
35+
edge_label: str | dict[tuple[_Node, _Node], str] = "edge_label",
36+
edge_label_options: str | dict[tuple[_Node, _Node], str] = "edge_label_options",
37+
caption: str = "",
38+
latex_label: str = "",
39+
sub_captions: Collection[str] | None = None,
40+
sub_labels: Collection[str] | None = None,
41+
n_rows: int = 1,
42+
as_document: bool = True,
43+
document_wrapper: str = ...,
44+
figure_wrapper: str = ...,
45+
subfigure_wrapper: str = ...,
46+
) -> str: ...
47+
def write_latex(
48+
Gbunch: Graph[_Node] | Collection[Graph[_Node]],
49+
path: StrPath | SupportsWrite[str],
50+
*,
51+
# **options passed to `to_latex`
52+
pos: _Pos[_Node, _PosT] | Collection[_Pos[_Node, _PosT]] = "pos",
1853
tikz_options: str = "",
1954
default_node_options: str = "",
20-
node_options: str = "node_options",
21-
node_label: str = "node_label",
55+
node_options: str | dict[_Node, str] = "node_options",
56+
node_label: str | dict[_Node, str] = "node_label",
2257
default_edge_options: str = "",
23-
edge_options: str = "edge_options",
24-
edge_label: str = "edge_label",
25-
edge_label_options: str = "edge_label_options",
58+
edge_options: str | dict[tuple[_Node, _Node], str] = "edge_options",
59+
edge_label: str | dict[tuple[_Node, _Node], str] = "edge_label",
60+
edge_label_options: str | dict[tuple[_Node, _Node], str] = "edge_label_options",
2661
caption: str = "",
2762
latex_label: str = "",
28-
sub_captions=None,
29-
sub_labels=None,
63+
sub_captions: Collection[str] | None = None,
64+
sub_labels: Collection[str] | None = None,
3065
n_rows: int = 1,
3166
as_document: bool = True,
3267
document_wrapper: str = ...,
3368
figure_wrapper: str = ...,
3469
subfigure_wrapper: str = ...,
35-
): ...
36-
def write_latex(Gbunch, path, **options) -> None: ...
70+
) -> None: ...

0 commit comments

Comments
 (0)