Skip to content

Commit 4df58d8

Browse files
authored
Complete networkx/graph.pyi. #14499 (#14509)
1 parent 562fa25 commit 4df58d8

File tree

3 files changed

+43
-25
lines changed

3 files changed

+43
-25
lines changed

stubs/networkx/networkx/classes/graph.pyi

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from _typeshed import Incomplete
21
from collections.abc import Callable, Collection, Hashable, Iterable, Iterator, Mapping, MutableMapping
32
from functools import cached_property
43
from typing import Any, ClassVar, TypeVar, overload
@@ -7,7 +6,7 @@ from typing_extensions import Self, TypeAlias
76
import numpy
87
from networkx.classes.coreviews import AdjacencyView, AtlasView
98
from networkx.classes.digraph import DiGraph
10-
from networkx.classes.reportviews import DegreeView, DiDegreeView, NodeView, OutEdgeView
9+
from networkx.classes.reportviews import DegreeView, EdgeView, NodeView
1110

1211
_Node = TypeVar("_Node", bound=Hashable)
1312
_NodeWithData: TypeAlias = tuple[_Node, dict[str, Any]]
@@ -38,12 +37,14 @@ class Graph(Collection[_Node]):
3837
graph_attr_dict_factory: ClassVar[_MapFactory]
3938

4039
graph: dict[str, Any]
40+
__networkx_cache__: dict[str, Any]
4141

4242
def to_directed_class(self) -> type[DiGraph[_Node]]: ...
4343
def to_undirected_class(self) -> type[Graph[_Node]]: ...
44-
def __init__(self, incoming_graph_data: _Data[_Node] | None = None, **attr) -> None: ...
44+
def __init__(self, incoming_graph_data: _Data[_Node] | None = None, **attr: Any) -> None: ... # attr: key=value pairs
4545
@cached_property
46-
def adj(self) -> AdjacencyView[_Node, _Node, dict[str, Incomplete]]: ...
46+
def adj(self) -> AdjacencyView[_Node, _Node, Mapping[str, Any]]: ...
47+
# This object is a read-only dict-like structure
4748
@property
4849
def name(self) -> str: ...
4950
@name.setter
@@ -53,20 +54,22 @@ class Graph(Collection[_Node]):
5354
def __contains__(self, n: object) -> bool: ...
5455
def __len__(self) -> int: ...
5556
def add_node(self, node_for_adding: _Node, **attr: Any) -> None: ... # attr: Set or change node attributes using key=value
56-
def add_nodes_from(self, nodes_for_adding: Iterable[_NodePlus[_Node]], **attr) -> None: ...
57+
def add_nodes_from(self, nodes_for_adding: Iterable[_NodePlus[_Node]], **attr: Any) -> None: ... # attr: key=value pairs
5758
def remove_node(self, n: _Node) -> None: ...
5859
def remove_nodes_from(self, nodes: Iterable[_Node]) -> None: ...
5960
@cached_property
6061
def nodes(self) -> NodeView[_Node]: ...
6162
def number_of_nodes(self) -> int: ...
6263
def order(self) -> int: ...
6364
def has_node(self, n: _Node) -> bool: ...
64-
# attr: Edge data (or labels or objects) can be assigned using keyword arguments
6565
def add_edge(self, u_of_edge: _Node, v_of_edge: _Node, **attr: Any) -> None: ...
66-
def add_edges_from(self, ebunch_to_add: Iterable[_EdgePlus[_Node]], **attr) -> None: ...
66+
# attr: Edge data (or labels or objects) can be assigned using keyword arguments
67+
def add_edges_from(self, ebunch_to_add: Iterable[_EdgePlus[_Node]], **attr: Any) -> None: ...
68+
# attr: Edge data (or labels or objects) can be assigned using keyword arguments
6769
def add_weighted_edges_from(
68-
self, ebunch_to_add: Iterable[tuple[_Node, _Node, Incomplete]], weight: str = "weight", **attr
70+
self, ebunch_to_add: Iterable[tuple[_Node, _Node, float]], weight: str = "weight", **attr: Any
6971
) -> None: ...
72+
# attr: Edge attributes to add/update for all edges.
7073
def remove_edge(self, u: _Node, v: _Node) -> None: ...
7174
def remove_edges_from(self, ebunch: Iterable[_EdgePlus[_Node]]) -> None: ...
7275
@overload
@@ -78,11 +81,12 @@ class Graph(Collection[_Node]):
7881
def has_edge(self, u: _Node, v: _Node) -> bool: ...
7982
def neighbors(self, n: _Node) -> Iterator[_Node]: ...
8083
@cached_property
81-
def edges(self) -> OutEdgeView[_Node]: ...
82-
def get_edge_data(self, u: _Node, v: _Node, default=None) -> Mapping[str, Incomplete]: ...
83-
def adjacency(self) -> Iterator[tuple[_Node, Mapping[_Node, Mapping[str, Incomplete]]]]: ...
84+
def edges(self) -> EdgeView[_Node]: ...
85+
def get_edge_data(self, u: _Node, v: _Node, default: Any = None) -> Mapping[str, Any]: ...
86+
# default: any Python object
87+
def adjacency(self) -> Iterator[tuple[_Node, Mapping[_Node, Mapping[str, Any]]]]: ...
8488
@cached_property
85-
def degree(self) -> DegreeView[_Node] | DiDegreeView[_Node]: ... # Include subtypes' possible return types
89+
def degree(self) -> int | DegreeView[_Node]: ...
8690
def clear(self) -> None: ...
8791
def clear_edges(self) -> None: ...
8892
def is_multigraph(self) -> bool: ...

stubs/networkx/networkx/classes/multidigraph.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ class MultiDiGraph(MultiGraph[_Node], DiGraph[_Node]):
1515
@cached_property
1616
def pred(self) -> MultiAdjacencyView[_Node, _Node, dict[str, Incomplete]]: ...
1717
@cached_property
18-
def edges(self) -> OutMultiEdgeView[_Node]: ...
18+
def edges(self) -> OutMultiEdgeView[_Node]: ... # type: ignore[override]
19+
# Returns: OutMultiEdgeView
1920
@cached_property
2021
def out_edges(self) -> OutMultiEdgeView[_Node]: ...
2122
@cached_property
Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,46 @@
1-
from _typeshed import Incomplete
2-
from collections.abc import Mapping
1+
from collections.abc import Hashable, Mapping
32
from functools import cached_property
4-
from typing import ClassVar
5-
from typing_extensions import TypeAlias
3+
from typing import Any, ClassVar, overload
4+
from typing_extensions import TypeAlias, TypeVar
65

76
from networkx.classes.coreviews import MultiAdjacencyView
87
from networkx.classes.graph import Graph, _MapFactory, _Node
98
from networkx.classes.multidigraph import MultiDiGraph
10-
from networkx.classes.reportviews import OutMultiEdgeView
9+
from networkx.classes.reportviews import MultiEdgeView
1110

1211
_MultiEdge: TypeAlias = tuple[_Node, _Node, int] # noqa: Y047
1312

13+
_Any = TypeVar("_Any")
14+
1415
__all__ = ["MultiGraph"]
1516

1617
class MultiGraph(Graph[_Node]):
1718
edge_key_dict_factory: ClassVar[_MapFactory]
18-
def __init__(self, incoming_graph_data=None, multigraph_input: bool | None = None, **attr) -> None: ...
19+
def __init__(self, incoming_graph_data=None, multigraph_input: bool | None = None, **attr: Any) -> None: ...
1920
@cached_property
20-
def adj(self) -> MultiAdjacencyView[_Node, _Node, dict[str, Incomplete]]: ...
21+
def adj(self) -> MultiAdjacencyView[_Node, _Node, Mapping[str, Any]]: ...
2122
def new_edge_key(self, u: _Node, v: _Node) -> int: ...
22-
def add_edge(self, u_for_edge, v_for_edge, key=None, **attr): ... # type: ignore[override] # Has an additional `key` keyword argument
23+
def add_edge( # type: ignore[override]
24+
self, u_for_edge: _Node, v_for_edge: _Node, key: Hashable | int | None = None, **attr: Any
25+
) -> Hashable | int: ...
26+
# key : hashable identifier, optional (default=lowest unused integer)
2327
def remove_edge(self, u, v, key=None): ...
2428
def has_edge(self, u: _Node, v: _Node, key=None) -> bool: ...
25-
def get_edge_data( # type: ignore[override] # Has an additional `key` keyword argument
26-
self, u, v, key=None, default=None
27-
) -> Mapping[str, Incomplete]: ...
29+
@overload # type: ignore[override]
30+
def get_edge_data(self, u: _Node, v: _Node, key: Hashable, default: _Any | None = None) -> Mapping[str, Any] | _Any: ...
31+
# key : hashable identifier, optional (default=None).
32+
# default : any Python object (default=None). Value to return if the specific edge (u, v, key) is not found.
33+
# Returns: The edge attribute dictionary.
34+
@overload
35+
def get_edge_data(
36+
self, u: _Node, v: _Node, key: None = None, default: _Any | None = None
37+
) -> Mapping[Hashable, Mapping[str, Any] | _Any]: ...
38+
# default : any Python object (default=None). Value to return if there are no edges between u and v and no key is specified.
39+
# Returns: A dictionary mapping edge keys to attribute dictionaries for each of those edges if no specific key is provided.
2840
def copy(self, as_view: bool = False) -> MultiGraph[_Node]: ...
2941
def to_directed(self, as_view: bool = False) -> MultiDiGraph[_Node]: ...
3042
def to_undirected(self, as_view: bool = False) -> MultiGraph[_Node]: ...
3143
def number_of_edges(self, u: _Node | None = None, v: _Node | None = None) -> int: ...
3244
@cached_property
33-
def edges(self) -> OutMultiEdgeView[_Node]: ...
45+
def edges(self) -> MultiEdgeView[_Node]: ... # type: ignore[override]
46+
# Returns: MultiEdgeView

0 commit comments

Comments
 (0)