Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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]]])
70 changes: 60 additions & 10 deletions stubs/networkx/networkx/algorithms/shortest_paths/weighted.pyi
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
Loading