From 08fb2e563bc4f0e21b96b16d2673c124da0301e7 Mon Sep 17 00:00:00 2001 From: Ali Hamdan Date: Sat, 2 Aug 2025 13:44:54 +0200 Subject: [PATCH] networkx: improve the shortest_paths.weighted module --- .../algorithms/shortest_paths/weighted.pyi | 152 +++++++----------- 1 file changed, 55 insertions(+), 97 deletions(-) diff --git a/stubs/networkx/networkx/algorithms/shortest_paths/weighted.pyi b/stubs/networkx/networkx/algorithms/shortest_paths/weighted.pyi index 39284b9cce64..5eb7445b0065 100644 --- a/stubs/networkx/networkx/algorithms/shortest_paths/weighted.pyi +++ b/stubs/networkx/networkx/algorithms/shortest_paths/weighted.pyi @@ -1,6 +1,6 @@ -from _typeshed import Incomplete, SupportsGetItem -from collections.abc import Callable, Generator +from collections.abc import Callable, Collection, Generator from typing import Any +from typing_extensions import TypeAlias from networkx.classes.graph import Graph, _Node from networkx.utils.backends import _dispatchable @@ -33,156 +33,114 @@ __all__ = [ "johnson", ] +_WeightFunc: TypeAlias = Callable[ + [_Node, _Node, dict[str, Any]], # Any: type of edge data cannot be known statically + float | None, # the weight or None to indicate a hidden edge +] + @_dispatchable def dijkstra_path( - G: Graph[_Node], - source: _Node, - target: _Node, - weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight", -) -> dict[Incomplete, list[Incomplete]] | list[Incomplete]: ... + G: Graph[_Node], source: _Node, target: _Node, weight: str | _WeightFunc[_Node] | None = "weight" +) -> list[_Node]: ... @_dispatchable def dijkstra_path_length( - G: Graph[_Node], - source: _Node, - target: _Node, - weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight", -): ... + G: Graph[_Node], source: _Node, target: _Node, weight: str | _WeightFunc[_Node] | None = "weight" +) -> float: ... @_dispatchable def single_source_dijkstra_path( - G: Graph[_Node], - source: _Node, - cutoff: float | None = None, - weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight", -) -> dict[Incomplete, list[Incomplete]] | list[Incomplete]: ... + G: Graph[_Node], source: _Node, cutoff: float | None = None, weight: str | _WeightFunc[_Node] | None = "weight" +) -> dict[_Node, list[_Node]]: ... @_dispatchable def single_source_dijkstra_path_length( - G: Graph[_Node], - source: _Node, - cutoff: float | None = None, - weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight", -) -> dict[Incomplete, Incomplete]: ... + G: Graph[_Node], source: _Node, cutoff: float | None = None, weight: str | _WeightFunc[_Node] | None = "weight" +) -> dict[_Node, float]: ... @_dispatchable def single_source_dijkstra( G: Graph[_Node], source: _Node, target: _Node | None = None, cutoff: float | None = None, - weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight", -) -> tuple[Incomplete, Incomplete]: ... + weight: str | _WeightFunc[_Node] | None = "weight", +) -> tuple[dict[_Node, float], dict[_Node, list[_Node]]] | tuple[float, list[_Node]]: ... # TODO: overload on target @_dispatchable def multi_source_dijkstra_path( - G: Graph[_Node], - sources, - cutoff: float | None = None, - weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight", -) -> dict[Incomplete, list[Incomplete]] | list[Incomplete]: ... + G: Graph[_Node], sources: Collection[_Node], cutoff: float | None = None, weight: str | _WeightFunc[_Node] | None = "weight" +) -> dict[_Node, list[_Node]]: ... @_dispatchable def multi_source_dijkstra_path_length( - G: Graph[_Node], - sources, - cutoff: float | None = None, - weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight", -) -> dict[Incomplete, Incomplete]: ... + G: Graph[_Node], sources: Collection[_Node], cutoff: float | None = None, weight: str | _WeightFunc[_Node] | None = "weight" +) -> dict[_Node, float]: ... @_dispatchable def multi_source_dijkstra( G: Graph[_Node], - sources, + sources: Collection[_Node], target: _Node | None = None, cutoff: float | None = None, - weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight", -) -> tuple[Incomplete, Incomplete]: ... + weight: str | _WeightFunc[_Node] | None = "weight", +) -> tuple[dict[_Node, float], dict[_Node, list[_Node]]] | tuple[float, list[_Node]]: ... # TODO: overload on target @_dispatchable def dijkstra_predecessor_and_distance( - G: Graph[_Node], - source: _Node, - cutoff: float | None = None, - weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight", -) -> tuple[dict[Incomplete, list[Incomplete]], dict[Incomplete, Incomplete]]: ... + G: Graph[_Node], source: _Node, cutoff: float | None = None, weight: str | _WeightFunc[_Node] | None = "weight" +) -> tuple[dict[_Node, list[_Node]], dict[_Node, float]]: ... @_dispatchable def all_pairs_dijkstra( - G: Graph[_Node], - cutoff: float | None = None, - weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight", -) -> Generator[Incomplete, None, None]: ... + G: Graph[_Node], cutoff: float | None = None, weight: str | _WeightFunc[_Node] | None = "weight" +) -> Generator[tuple[_Node, tuple[dict[_Node, float], dict[_Node, list[_Node]]]]]: ... @_dispatchable def all_pairs_dijkstra_path_length( - G: Graph[_Node], - cutoff: float | None = None, - weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight", -) -> Generator[Incomplete, None, None]: ... + G: Graph[_Node], cutoff: float | None = None, weight: str | _WeightFunc[_Node] | None = "weight" +) -> Generator[tuple[_Node, dict[_Node, float]]]: ... @_dispatchable def all_pairs_dijkstra_path( - G: Graph[_Node], - cutoff: float | None = None, - weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight", -) -> Generator[tuple[Incomplete, Incomplete], None, None]: ... + G: Graph[_Node], cutoff: float | None = None, weight: str | _WeightFunc[_Node] | None = "weight" +) -> Generator[tuple[_Node, dict[_Node, list[_Node]]]]: ... @_dispatchable def bellman_ford_predecessor_and_distance( G: Graph[_Node], source: _Node, target: _Node | None = None, - weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight", + weight: str | _WeightFunc[_Node] | None = "weight", heuristic: bool = False, -) -> tuple[Incomplete, Incomplete]: ... +) -> tuple[dict[_Node, list[_Node]], dict[_Node, float]]: ... @_dispatchable def bellman_ford_path( - G: Graph[_Node], - source: _Node, - target: _Node, - weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight", -) -> list[Incomplete] | dict[Incomplete, list[Incomplete]]: ... + G: Graph[_Node], source: _Node, target: _Node, weight: str | _WeightFunc[_Node] | None = "weight" +) -> list[_Node]: ... @_dispatchable def bellman_ford_path_length( - G: Graph[_Node], - source: _Node, - target: _Node, - weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight", -): ... + G: Graph[_Node], source: _Node, target: _Node, weight: str | _WeightFunc[_Node] | None = "weight" +) -> float: ... @_dispatchable def single_source_bellman_ford_path( - G: Graph[_Node], source: _Node, weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight" -) -> list[Incomplete] | dict[Incomplete, list[Incomplete]]: ... + G: Graph[_Node], source: _Node, weight: str | _WeightFunc[_Node] | None = "weight" +) -> dict[_Node, list[_Node]]: ... @_dispatchable def single_source_bellman_ford_path_length( - G: Graph[_Node], source: _Node, weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight" -) -> dict[Incomplete, int]: ... + G: Graph[_Node], source: _Node, weight: str | _WeightFunc[_Node] | None = "weight" +) -> dict[_Node, float]: ... @_dispatchable def single_source_bellman_ford( - G: Graph[_Node], - source: _Node, - target: _Node | None = None, - weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight", -): ... + 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 @_dispatchable def all_pairs_bellman_ford_path_length( - G: Graph[_Node], weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight" -) -> Generator[Incomplete, None, None]: ... + G: Graph[_Node], weight: str | _WeightFunc[_Node] | None = "weight" +) -> Generator[tuple[_Node, dict[_Node, float]]]: ... @_dispatchable def all_pairs_bellman_ford_path( - G: Graph[_Node], weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight" -) -> Generator[tuple[Incomplete, Incomplete], None, None]: ... + G: Graph[_Node], weight: str | _WeightFunc[_Node] | None = "weight" +) -> Generator[tuple[_Node, dict[_Node, list[_Node]]]]: ... @_dispatchable def goldberg_radzik( - G: Graph[_Node], source: _Node, weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight" -) -> tuple[dict[Incomplete, None], dict[Incomplete, int | float]]: ... + G: Graph[_Node], source: _Node, weight: str | _WeightFunc[_Node] | None = "weight" +) -> tuple[dict[_Node, _Node | None], dict[_Node, float]]: ... @_dispatchable -def negative_edge_cycle( - G: Graph[_Node], - weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight", - heuristic: bool = True, -): ... +def negative_edge_cycle(G: Graph[_Node], weight: str | _WeightFunc[_Node] | None = "weight", heuristic: bool = True) -> bool: ... @_dispatchable -def find_negative_cycle( - G: Graph[_Node], source: _Node, weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight" -): ... +def find_negative_cycle(G: Graph[_Node], source: _Node, weight: str | _WeightFunc[_Node] | None = "weight") -> list[_Node]: ... @_dispatchable def bidirectional_dijkstra( - G: Graph[_Node], - source: _Node, - target: _Node, - weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight", -): ... + G: Graph[_Node], source: _Node, target: _Node, weight: str | _WeightFunc[_Node] | None = "weight" +) -> tuple[float, list[_Node]]: ... @_dispatchable -def johnson( - G: Graph[_Node], weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight" -) -> dict[Any, dict[Any, list[Any]]]: ... +def johnson(G: Graph[_Node], weight: str | _WeightFunc[_Node] | None = "weight") -> dict[_Node, dict[_Node, list[_Node]]]: ...