diff --git a/stubs/networkx/@tests/test_cases/check_single_source_target_overloads-py312.py b/stubs/networkx/@tests/test_cases/check_single_source_target_overloads-py312.py new file mode 100644 index 000000000000..5df7fcb097aa --- /dev/null +++ b/stubs/networkx/@tests/test_cases/check_single_source_target_overloads-py312.py @@ -0,0 +1,15 @@ +from typing_extensions import assert_type + +import networkx as nx + +G: "nx.Graph[int]" = nx.Graph([(1, 2), (2, 3), (3, 4)]) + +# With target => (distance, path) +assert_type(nx.single_source_dijkstra(G, 1, 4), tuple[float, list[int]]) +assert_type(nx.single_source_bellman_ford(G, 1, 4), tuple[float, list[int]]) +assert_type(nx.multi_source_dijkstra(G, {1, 2}, 4), tuple[float, list[int]]) + +# Without target => (distances, paths) +assert_type(nx.single_source_dijkstra(G, 1), tuple[dict[int, float], dict[int, list[int]]]) +assert_type(nx.single_source_bellman_ford(G, 1), tuple[dict[int, float], dict[int, list[int]]]) +assert_type(nx.multi_source_dijkstra(G, {1, 2}), tuple[dict[int, float], dict[int, list[int]]]) diff --git a/stubs/networkx/networkx/algorithms/shortest_paths/weighted.pyi b/stubs/networkx/networkx/algorithms/shortest_paths/weighted.pyi index bee3ecf7470c..a890a356b585 100644 --- a/stubs/networkx/networkx/algorithms/shortest_paths/weighted.pyi +++ b/stubs/networkx/networkx/algorithms/shortest_paths/weighted.pyi @@ -1,5 +1,5 @@ from collections.abc import Callable, Collection, Generator -from typing import Any, TypeAlias +from typing import Any, TypeAlias, overload from networkx.classes.graph import Graph, _Node from networkx.utils.backends import _dispatchable @@ -53,14 +53,30 @@ def single_source_dijkstra_path( def single_source_dijkstra_path_length( G: Graph[_Node], source: _Node, cutoff: float | None = None, weight: str | _WeightFunc[_Node] | None = "weight" ) -> dict[_Node, float]: ... -@_dispatchable + +@overload # target is None => (distances, paths) def single_source_dijkstra( G: Graph[_Node], source: _Node, - target: _Node | None = None, + target: None = None, + cutoff: float | None = None, + weight: str | _WeightFunc[_Node] | None = "weight", + *, + backend: str | None = None, + **backend_kwargs: Any, +) -> tuple[dict[_Node, float], dict[_Node, list[_Node]]]: ... +@overload # target is specified => (distance, path) +def single_source_dijkstra( + G: Graph[_Node], + source: _Node, + target: _Node, cutoff: float | None = None, weight: str | _WeightFunc[_Node] | None = "weight", -) -> tuple[dict[_Node, float], dict[_Node, list[_Node]]] | tuple[float, list[_Node]]: ... # TODO: overload on target + *, + backend: str | None = None, + **backend_kwargs: Any, +) -> tuple[float, list[_Node]]: ... + @_dispatchable def multi_source_dijkstra_path( G: Graph[_Node], sources: Collection[_Node], cutoff: float | None = None, weight: str | _WeightFunc[_Node] | None = "weight" @@ -69,14 +85,30 @@ def multi_source_dijkstra_path( def multi_source_dijkstra_path_length( G: Graph[_Node], sources: Collection[_Node], cutoff: float | None = None, weight: str | _WeightFunc[_Node] | None = "weight" ) -> dict[_Node, float]: ... -@_dispatchable + +@overload # target is None => (distances, paths) def multi_source_dijkstra( G: Graph[_Node], sources: Collection[_Node], - target: _Node | None = None, + target: None = None, cutoff: float | None = None, weight: str | _WeightFunc[_Node] | None = "weight", -) -> tuple[dict[_Node, float], dict[_Node, list[_Node]]] | tuple[float, list[_Node]]: ... # TODO: overload on target + *, + backend: str | None = None, + **backend_kwargs: Any, +) -> tuple[dict[_Node, float], dict[_Node, list[_Node]]]: ... +@overload # target is specified => (distance, path) +def multi_source_dijkstra( + G: Graph[_Node], + sources: Collection[_Node], + target: _Node, + cutoff: float | None = None, + weight: str | _WeightFunc[_Node] | None = "weight", + *, + backend: str | None = None, + **backend_kwargs: Any, +) -> tuple[float, list[_Node]]: ... + @_dispatchable def dijkstra_predecessor_and_distance( G: Graph[_Node], source: _Node, cutoff: float | None = None, weight: str | _WeightFunc[_Node] | None = "weight" @@ -117,10 +149,28 @@ def single_source_bellman_ford_path( def single_source_bellman_ford_path_length( G: Graph[_Node], source: _Node, weight: str | _WeightFunc[_Node] | None = "weight" ) -> dict[_Node, float]: ... -@_dispatchable + +@overload # target is None => (distances, paths) def single_source_bellman_ford( - G: Graph[_Node], source: _Node, target: _Node | None = None, weight: str | _WeightFunc[_Node] | None = "weight" -) -> tuple[dict[_Node, float], dict[_Node, list[_Node]]] | tuple[float, list[_Node]]: ... # TODO: overload on target + G: Graph[_Node], + source: _Node, + target: None = None, + weight: str | _WeightFunc[_Node] | None = "weight", + *, + backend: str | None = None, + **backend_kwargs: Any, +) -> tuple[dict[_Node, float], dict[_Node, list[_Node]]]: ... +@overload # target is specified => (distance, path) +def single_source_bellman_ford( + G: Graph[_Node], + source: _Node, + target: _Node, + weight: str | _WeightFunc[_Node] | None = "weight", + *, + backend: str | None = None, + **backend_kwargs: Any, +) -> tuple[float, list[_Node]]: ... + @_dispatchable def all_pairs_bellman_ford_path_length( G: Graph[_Node], weight: str | _WeightFunc[_Node] | None = "weight"