From af88af1fa65c0dbc12765b8306c545812f3304aa Mon Sep 17 00:00:00 2001 From: Daniel Darabos Date: Tue, 23 Apr 2024 11:48:49 +0200 Subject: [PATCH] Annotate a few NetworkX algorithm types (#11811) --- .../algorithms/centrality/betweenness.pyi | 16 +++++++++------- .../algorithms/centrality/betweenness_subset.pyi | 11 ++++++++--- .../networkx/algorithms/centrality/closeness.pyi | 15 +++++++++++---- .../algorithms/centrality/degree_alg.pyi | 7 ++++--- .../algorithms/centrality/dispersion.pyi | 11 +++++------ stubs/networkx/networkx/convert.pyi | 10 +++++----- 6 files changed, 42 insertions(+), 28 deletions(-) diff --git a/stubs/networkx/networkx/algorithms/centrality/betweenness.pyi b/stubs/networkx/networkx/algorithms/centrality/betweenness.pyi index 5d638ff22..d69156f65 100644 --- a/stubs/networkx/networkx/algorithms/centrality/betweenness.pyi +++ b/stubs/networkx/networkx/algorithms/centrality/betweenness.pyi @@ -1,17 +1,19 @@ from _typeshed import Incomplete +from networkx.classes.graph import Graph, _Edge, _Node from networkx.utils.backends import _dispatch +from numpy.random import RandomState @_dispatch def betweenness_centrality( - G, - k: Incomplete | None = None, + G: Graph[_Node], + k: int | None = None, normalized: bool = True, - weight: Incomplete | None = None, + weight: str | None = None, endpoints: bool = False, - seed: Incomplete | None = None, -): ... + seed: int | RandomState | None = None, +) -> dict[_Node, float]: ... @_dispatch def edge_betweenness_centrality( - G, k: Incomplete | None = None, normalized: bool = True, weight: Incomplete | None = None, seed: Incomplete | None = None -): ... + G: Graph[_Node], k: int | None = None, normalized: bool = True, weight: str | None = None, seed: Incomplete | None = None +) -> dict[_Edge[_Node], float]: ... diff --git a/stubs/networkx/networkx/algorithms/centrality/betweenness_subset.pyi b/stubs/networkx/networkx/algorithms/centrality/betweenness_subset.pyi index 2c9d29473..d1c012c8e 100644 --- a/stubs/networkx/networkx/algorithms/centrality/betweenness_subset.pyi +++ b/stubs/networkx/networkx/algorithms/centrality/betweenness_subset.pyi @@ -1,8 +1,13 @@ -from _typeshed import Incomplete +from collections.abc import Iterable +from networkx.classes.graph import Graph, _Edge, _Node from networkx.utils.backends import _dispatch @_dispatch -def betweenness_centrality_subset(G, sources, targets, normalized: bool = False, weight: Incomplete | None = None): ... +def betweenness_centrality_subset( + G: Graph[_Node], sources: Iterable[_Node], targets: Iterable[_Node], normalized: bool = False, weight: str | None = None +) -> dict[_Node, float]: ... @_dispatch -def edge_betweenness_centrality_subset(G, sources, targets, normalized: bool = False, weight: Incomplete | None = None): ... +def edge_betweenness_centrality_subset( + G: Graph[_Node], sources: Iterable[_Node], targets: Iterable[_Node], normalized: bool = False, weight: str | None = None +) -> dict[_Edge[_Node], float]: ... diff --git a/stubs/networkx/networkx/algorithms/centrality/closeness.pyi b/stubs/networkx/networkx/algorithms/centrality/closeness.pyi index cc7ca02da..4a93551b6 100644 --- a/stubs/networkx/networkx/algorithms/centrality/closeness.pyi +++ b/stubs/networkx/networkx/algorithms/centrality/closeness.pyi @@ -1,10 +1,17 @@ -from _typeshed import Incomplete +from _typeshed import SupportsGetItem +from networkx.classes.graph import Graph, _Edge, _Node from networkx.utils.backends import _dispatch @_dispatch -def closeness_centrality(G, u: Incomplete | None = None, distance: Incomplete | None = None, wf_improved: bool = True): ... +def closeness_centrality( + G: Graph[_Node], u: _Node | None = None, distance: str | None = None, wf_improved: bool = True +) -> dict[_Node, float]: ... @_dispatch def incremental_closeness_centrality( - G, edge, prev_cc: Incomplete | None = None, insertion: bool = True, wf_improved: bool = True -): ... + G: Graph[_Node], + edge: _Edge[_Node], + prev_cc: SupportsGetItem[_Node, float] | None = None, + insertion: bool = True, + wf_improved: bool = True, +) -> dict[_Node, float]: ... diff --git a/stubs/networkx/networkx/algorithms/centrality/degree_alg.pyi b/stubs/networkx/networkx/algorithms/centrality/degree_alg.pyi index 19fa9b606..94013e318 100644 --- a/stubs/networkx/networkx/algorithms/centrality/degree_alg.pyi +++ b/stubs/networkx/networkx/algorithms/centrality/degree_alg.pyi @@ -1,8 +1,9 @@ +from networkx.classes.graph import Graph, _Node from networkx.utils.backends import _dispatch @_dispatch -def degree_centrality(G): ... +def degree_centrality(G: Graph[_Node]) -> dict[_Node, float]: ... @_dispatch -def in_degree_centrality(G): ... +def in_degree_centrality(G: Graph[_Node]) -> dict[_Node, float]: ... @_dispatch -def out_degree_centrality(G): ... +def out_degree_centrality(G: Graph[_Node]) -> dict[_Node, float]: ... diff --git a/stubs/networkx/networkx/algorithms/centrality/dispersion.pyi b/stubs/networkx/networkx/algorithms/centrality/dispersion.pyi index 440a0f3c5..c0550d148 100644 --- a/stubs/networkx/networkx/algorithms/centrality/dispersion.pyi +++ b/stubs/networkx/networkx/algorithms/centrality/dispersion.pyi @@ -1,14 +1,13 @@ -from _typeshed import Incomplete - +from networkx.classes.graph import Graph, _Node from networkx.utils.backends import _dispatch @_dispatch def dispersion( - G, - u: Incomplete | None = None, - v: Incomplete | None = None, + G: Graph[_Node], + u: _Node | None = None, + v: _Node | None = None, normalized: bool = True, alpha: float = 1.0, b: float = 0.0, c: float = 0.0, -): ... +) -> dict[_Node, float] | dict[_Node, dict[_Node, float]]: ... diff --git a/stubs/networkx/networkx/convert.pyi b/stubs/networkx/networkx/convert.pyi index db9036810..ab291b08f 100644 --- a/stubs/networkx/networkx/convert.pyi +++ b/stubs/networkx/networkx/convert.pyi @@ -20,11 +20,11 @@ def to_networkx_graph( @_dispatch def to_dict_of_lists(G: Graph[_Node], nodelist: None | Iterable[_Node] = None) -> dict[_Node, list[_Node]]: ... @_dispatch -def from_dict_of_lists(d: dict[_Node, Iterable[_Node]], create_using: Incomplete | None = None): ... -def to_dict_of_dicts(G, nodelist=None, edge_data=None) -> dict[Incomplete, Incomplete]: ... +def from_dict_of_lists(d: dict[_Node, Iterable[_Node]], create_using: Incomplete | None = None) -> Graph[_Node]: ... +def to_dict_of_dicts(G: Graph[_Node], nodelist=None, edge_data=None) -> dict[Incomplete, Incomplete]: ... @_dispatch -def from_dict_of_dicts(d, create_using=None, multigraph_input=False): ... +def from_dict_of_dicts(d, create_using=None, multigraph_input=False) -> Graph[Incomplete]: ... @_dispatch -def to_edgelist(G, nodelist=None): ... +def to_edgelist(G: Graph[_Node], nodelist=None): ... @_dispatch -def from_edgelist(edgelist, create_using=None): ... +def from_edgelist(edgelist, create_using=None) -> Graph[Incomplete]: ...