From 9b534e1037d89783292003ea8fc4007a593e3c00 Mon Sep 17 00:00:00 2001 From: Vladimir Babin Date: Tue, 10 Nov 2026 06:19:21 +0800 Subject: [PATCH 1/2] Overload single_source/multi_source funcs on target These three functions return (distance, path) when target is given and (distances, paths) otherwise, but the stubs returned the union of both so callers had to narrow manually. Split each into @overload on target, mirroring the existing shortest_path overloads in generic.pyi, and drop the stale # TODO: overload on target comments. Fixes #16744 --- ...ck_single_source_target_overloads-py312.py | 15 +++++ .../algorithms/shortest_paths/weighted.pyi | 64 ++++++++++++++++--- 2 files changed, 69 insertions(+), 10 deletions(-) create mode 100644 stubs/networkx/@tests/test_cases/check_single_source_target_overloads-py312.py 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..55f1d294b52f 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,28 @@ 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 +83,28 @@ 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 +145,26 @@ 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: 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 | 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: _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" From 02369658a84d1ab4bce489f8fffbbad7c161bffc Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 9 Nov 2026 22:21:55 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks --- .../networkx/algorithms/shortest_paths/weighted.pyi | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/stubs/networkx/networkx/algorithms/shortest_paths/weighted.pyi b/stubs/networkx/networkx/algorithms/shortest_paths/weighted.pyi index 55f1d294b52f..a890a356b585 100644 --- a/stubs/networkx/networkx/algorithms/shortest_paths/weighted.pyi +++ b/stubs/networkx/networkx/algorithms/shortest_paths/weighted.pyi @@ -53,6 +53,7 @@ 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]: ... + @overload # target is None => (distances, paths) def single_source_dijkstra( G: Graph[_Node], @@ -75,6 +76,7 @@ def single_source_dijkstra( 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" @@ -83,6 +85,7 @@ 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]: ... + @overload # target is None => (distances, paths) def multi_source_dijkstra( G: Graph[_Node], @@ -105,6 +108,7 @@ def multi_source_dijkstra( 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" @@ -145,6 +149,7 @@ 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]: ... + @overload # target is None => (distances, paths) def single_source_bellman_ford( G: Graph[_Node], @@ -165,6 +170,7 @@ def single_source_bellman_ford( 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"