From e66905bead6bc07c43ea026d042b945fd31539db Mon Sep 17 00:00:00 2001 From: Kayokoi <113896798+wozhenbang2004@users.noreply.github.com> Date: Mon, 23 Nov 2026 17:15:39 +0800 Subject: [PATCH 1/2] [networkx] Accept custom mappings in unweighted shortest paths --- .../algorithms/shortest_paths/unweighted.pyi | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/stubs/networkx/networkx/algorithms/shortest_paths/unweighted.pyi b/stubs/networkx/networkx/algorithms/shortest_paths/unweighted.pyi index 636eb613875d..d315cbcca09a 100644 --- a/stubs/networkx/networkx/algorithms/shortest_paths/unweighted.pyi +++ b/stubs/networkx/networkx/algorithms/shortest_paths/unweighted.pyi @@ -1,7 +1,7 @@ from _typeshed import Incomplete from collections.abc import Generator -from networkx.classes.graph import Graph, _Node +from networkx.classes.graph import Graph, _EdgeData, _Node, _NodeData from networkx.utils.backends import _dispatchable __all__ = [ @@ -16,28 +16,34 @@ __all__ = [ ] @_dispatchable -def single_source_shortest_path_length(G: Graph[_Node], source: _Node, cutoff: int | None = None) -> dict[Incomplete, int]: ... +def single_source_shortest_path_length( + G: Graph[_Node, _NodeData, _EdgeData], source: _Node, cutoff: int | None = None +) -> dict[Incomplete, int]: ... @_dispatchable def single_target_shortest_path_length( - G: Graph[_Node], target: _Node, cutoff: int | None = None + G: Graph[_Node, _NodeData, _EdgeData], target: _Node, cutoff: int | None = None ) -> dict[Incomplete, Incomplete]: ... @_dispatchable -def all_pairs_shortest_path_length(G: Graph[_Node], cutoff: int | None = None) -> Generator[Incomplete]: ... +def all_pairs_shortest_path_length(G: Graph[_Node, _NodeData, _EdgeData], cutoff: int | None = None) -> Generator[Incomplete]: ... @_dispatchable -def bidirectional_shortest_path(G: Graph[_Node], source: _Node, target: _Node) -> list[Incomplete]: ... +def bidirectional_shortest_path(G: Graph[_Node, _NodeData, _EdgeData], source: _Node, target: _Node) -> list[Incomplete]: ... @_dispatchable def single_source_shortest_path( - G: Graph[_Node], source: _Node, cutoff: int | None = None + G: Graph[_Node, _NodeData, _EdgeData], source: _Node, cutoff: int | None = None ) -> dict[Incomplete, list[Incomplete]]: ... @_dispatchable def single_target_shortest_path( - G: Graph[_Node], target: _Node, cutoff: int | None = None + G: Graph[_Node, _NodeData, _EdgeData], target: _Node, cutoff: int | None = None ) -> dict[Incomplete, list[Incomplete]]: ... @_dispatchable def all_pairs_shortest_path( - G: Graph[_Node], cutoff: int | None = None + G: Graph[_Node, _NodeData, _EdgeData], cutoff: int | None = None ) -> Generator[tuple[Incomplete, dict[Incomplete, list[Incomplete]]]]: ... @_dispatchable def predecessor( - G: Graph[_Node], source: _Node, target: _Node | None = None, cutoff: int | None = None, return_seen: bool | None = None + G: Graph[_Node, _NodeData, _EdgeData], + source: _Node, + target: _Node | None = None, + cutoff: int | None = None, + return_seen: bool | None = None, ) -> dict[_Node, list[_Node]] | list[_Node] | tuple[dict[_Node, list[_Node]], dict[_Node, int]] | tuple[list[_Node], int]: ... From bdc2f3bf7c16b0ac71da79ada2266444d574c6b3 Mon Sep 17 00:00:00 2001 From: Kayokoi <113896798+wozhenbang2004@users.noreply.github.com> Date: Mon, 23 Nov 2026 17:22:59 +0800 Subject: [PATCH 2/2] [networkx] Add regression coverage for custom attribute mappings Implement unweighted path functions for custom attributes in a graph. --- .../test_cases/check_unweighted_paths-py312.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 stubs/networkx/@tests/test_cases/check_unweighted_paths-py312.py diff --git a/stubs/networkx/@tests/test_cases/check_unweighted_paths-py312.py b/stubs/networkx/@tests/test_cases/check_unweighted_paths-py312.py new file mode 100644 index 000000000000..3984046d051f --- /dev/null +++ b/stubs/networkx/@tests/test_cases/check_unweighted_paths-py312.py @@ -0,0 +1,15 @@ +from collections import UserDict +from typing import Any + +import networkx as nx + + +def unweighted_paths_with_custom_attributes(graph: nx.Graph[int, UserDict[str, Any], UserDict[str, Any]]) -> None: + nx.single_source_shortest_path_length(graph, 0) + nx.single_target_shortest_path_length(graph, 2) + nx.all_pairs_shortest_path_length(graph) + nx.bidirectional_shortest_path(graph, 0, 2) + nx.single_source_shortest_path(graph, 0) + nx.single_target_shortest_path(graph, 2) + nx.all_pairs_shortest_path(graph) + nx.predecessor(graph, 0)