Skip to content

Commit d876dba

Browse files
authored
networkx: complete the astar and generic modules in shortest_paths (#14550)
1 parent 7ebff91 commit d876dba

File tree

3 files changed

+112
-40
lines changed

3 files changed

+112
-40
lines changed

stubs/networkx/@tests/stubtest_allowlist.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
networkx\.(algorithms\.)?(boundary\.)?edge_boundary
55
networkx\.(algorithms\.)?(bridges\.)?local_bridges
66
networkx\.(algorithms\.)?(clique\.)?node_clique_number
7-
networkx\.(algorithms\.)?(shortest_paths\.)?(generic\.)?shortest_path
87
networkx\.(convert_matrix\.)?from_numpy_array
98
networkx\.(convert_matrix\.)?from_pandas_adjacency
109
networkx\.(convert_matrix\.)?from_pandas_edgelist
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
from _typeshed import Incomplete, SupportsGetItem
21
from collections.abc import Callable
3-
from typing import Any
42

3+
from networkx.algorithms.shortest_paths.weighted import _WeightFunc
54
from networkx.classes.graph import Graph, _Node
65
from networkx.utils.backends import _dispatchable
76

@@ -12,18 +11,18 @@ def astar_path(
1211
G: Graph[_Node],
1312
source: _Node,
1413
target: _Node,
15-
heuristic: Callable[..., Incomplete] | None = None,
16-
weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight",
14+
heuristic: Callable[[_Node, _Node], float] | None = None,
15+
weight: str | _WeightFunc[_Node] | None = "weight",
1716
*,
1817
cutoff: float | None = None,
19-
): ...
18+
) -> list[_Node]: ...
2019
@_dispatchable
2120
def astar_path_length(
2221
G: Graph[_Node],
2322
source: _Node,
2423
target: _Node,
25-
heuristic: Callable[..., Incomplete] | None = None,
26-
weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight",
24+
heuristic: Callable[[_Node, _Node], float] | None = None,
25+
weight: str | _WeightFunc[_Node] | None = "weight",
2726
*,
2827
cutoff: float | None = None,
29-
): ...
28+
) -> float: ...
Lines changed: 105 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from _typeshed import Incomplete
2-
from collections.abc import Callable, Generator
1+
from collections.abc import Generator
32
from typing import overload
43

4+
from networkx.algorithms.shortest_paths.weighted import _WeightFunc
55
from networkx.classes.graph import Graph, _Node
66
from networkx.utils.backends import _dispatchable
77

@@ -17,55 +17,129 @@ __all__ = [
1717

1818
@_dispatchable
1919
def has_path(G: Graph[_Node], source: _Node, target: _Node) -> bool: ...
20-
@overload
20+
@overload # both source and target are specified => (s -> t)
2121
def shortest_path(
2222
G: Graph[_Node],
23-
source: _Node | None = None,
24-
target: _Node | None = None,
25-
weight: str | Callable[..., Incomplete] | None = None,
23+
source: _Node,
24+
target: _Node,
25+
weight: str | _WeightFunc[_Node] | None = None,
2626
method: str | None = "dijkstra",
27+
*,
28+
backend: str | None = None,
29+
**backend_kwargs,
2730
) -> list[_Node]: ...
28-
@overload
31+
@overload # only source is specified => {t1: (s -> t), t2: (s -> t), ...}
2932
def shortest_path(
3033
G: Graph[_Node],
31-
source: _Node | None = None,
32-
target: _Node | None = None,
33-
weight: str | Callable[..., Incomplete] | None = None,
34+
source: _Node,
35+
target: None = None,
36+
weight: str | _WeightFunc[_Node] | None = None,
3437
method: str | None = "dijkstra",
38+
*,
39+
backend: str | None = None,
40+
**backend_kwargs,
3541
) -> dict[_Node, list[_Node]]: ...
36-
@overload
42+
@overload # only target is specified (positional) => {s1: (s1 -> t), s2: (s2 -> t), ...}
3743
def shortest_path(
3844
G: Graph[_Node],
39-
source: _Node | None = None,
40-
target: _Node | None = None,
41-
weight: str | Callable[..., Incomplete] | None = None,
45+
source: None,
46+
target: _Node,
47+
weight: str | _WeightFunc[_Node] | None = None,
4248
method: str | None = "dijkstra",
49+
*,
50+
backend: str | None = None,
51+
**backend_kwargs,
4352
) -> dict[_Node, list[_Node]]: ...
44-
@_dispatchable
53+
@overload # only target is specified (keyword) => {s1: (s1 -> t), s2: (s2 -> t), ...}
54+
def shortest_path(
55+
G: Graph[_Node],
56+
source: None = None,
57+
*,
58+
target: _Node,
59+
weight: str | _WeightFunc[_Node] | None = None,
60+
method: str | None = "dijkstra",
61+
backend: str | None = None,
62+
**backend_kwargs,
63+
) -> dict[_Node, list[_Node]]: ...
64+
@overload
65+
def shortest_path( # source and target are not specified => generator of (t, {s1: (s1 -> t), s2: (s2 -> t), ...})
66+
G: Graph[_Node],
67+
source: None = None,
68+
target: None = None,
69+
weight: str | _WeightFunc[_Node] | None = None,
70+
method: str | None = "dijkstra",
71+
*,
72+
backend: str | None = None,
73+
**backend_kwargs,
74+
) -> Generator[tuple[_Node, dict[str, list[_Node]]]]: ...
75+
@overload # both source and target are specified => len(s -> t)
4576
def shortest_path_length(
4677
G: Graph[_Node],
47-
source: _Node | None = None,
48-
target: _Node | None = None,
49-
weight: str | Callable[..., Incomplete] | None = None,
78+
source: _Node,
79+
target: _Node,
80+
weight: str | _WeightFunc[_Node] | None = None,
5081
method: str | None = "dijkstra",
51-
): ...
52-
@_dispatchable
53-
def average_shortest_path_length(
54-
G: Graph[_Node], weight: str | Callable[..., Incomplete] | None = None, method: str | None = None
55-
): ...
56-
@_dispatchable
57-
def all_shortest_paths(
82+
*,
83+
backend: str | None = None,
84+
**backend_kwargs,
85+
) -> float: ...
86+
@overload # only source is specified => {t1: len(s -> t1), t2: len(s -> t2), ...}
87+
def shortest_path_length(
5888
G: Graph[_Node],
5989
source: _Node,
90+
target: None = None,
91+
weight: str | _WeightFunc[_Node] | None = None,
92+
method: str | None = "dijkstra",
93+
*,
94+
backend: str | None = None,
95+
**backend_kwargs,
96+
) -> dict[_Node, float]: ...
97+
@overload # only target is specified (positional) => {s1: len(s1 -> t), s2: len(s2 -> t), ...}
98+
def shortest_path_length(
99+
G: Graph[_Node],
100+
source: None,
101+
target: _Node,
102+
weight: str | _WeightFunc[_Node] | None = None,
103+
method: str | None = "dijkstra",
104+
*,
105+
backend: str | None = None,
106+
**backend_kwargs,
107+
) -> dict[_Node, float]: ...
108+
@overload # only target is specified (keyword) => {s1: len(s1 -> t), s2: len(s2 -> t), ...}
109+
def shortest_path_length(
110+
G: Graph[_Node],
111+
source: None = None,
112+
*,
60113
target: _Node,
61-
weight: str | Callable[..., Incomplete] | None = None,
114+
weight: str | _WeightFunc[_Node] | None = None,
115+
method: str | None = "dijkstra",
116+
backend: str | None = None,
117+
**backend_kwargs,
118+
) -> dict[_Node, float]: ...
119+
@overload
120+
def shortest_path_length( # source and target are not specified => generator of (t, {s1: len(s1 -> t), s2: len(s2 -> t), ...})
121+
G: Graph[_Node],
122+
source: None = None,
123+
target: None = None,
124+
weight: str | _WeightFunc[_Node] | None = None,
62125
method: str | None = "dijkstra",
63-
) -> Generator[list[_Node], None, None]: ...
126+
*,
127+
backend: str | None = None,
128+
**backend_kwargs,
129+
) -> Generator[tuple[_Node, dict[_Node, float]]]: ...
130+
@_dispatchable
131+
def average_shortest_path_length(
132+
G: Graph[_Node], weight: str | _WeightFunc[_Node] | None = None, method: str | None = None
133+
) -> float: ...
134+
@_dispatchable
135+
def all_shortest_paths(
136+
G: Graph[_Node], source: _Node, target: _Node, weight: str | _WeightFunc[_Node] | None = None, method: str | None = "dijkstra"
137+
) -> Generator[list[_Node]]: ...
64138
@_dispatchable
65139
def single_source_all_shortest_paths(
66-
G, source, weight=None, method="dijkstra"
67-
) -> Generator[tuple[Incomplete, list[list[Incomplete]]]]: ...
140+
G: Graph[_Node], source: _Node, weight: str | _WeightFunc[_Node] | None = None, method: str | None = "dijkstra"
141+
) -> Generator[tuple[_Node, list[list[_Node]]]]: ...
68142
@_dispatchable
69143
def all_pairs_all_shortest_paths(
70-
G, weight=None, method="dijkstra"
71-
) -> Generator[tuple[Incomplete, dict[Incomplete, Incomplete]]]: ...
144+
G: Graph[_Node], weight: str | _WeightFunc[_Node] | None = None, method: str | None = "dijkstra"
145+
) -> Generator[tuple[_Node, dict[_Node, list[list[_Node]]]]]: ...

0 commit comments

Comments
 (0)