1- from collections .abc import Callable , Collection , Hashable , Iterable , Iterator , MutableMapping
1+ from collections .abc import Callable , Collection , Hashable , Iterable , Iterator , Mapping , MutableMapping
22from decimal import Decimal
33from functools import cached_property
4- from typing import Any , ClassVar , TypeAlias , TypeVar , overload
4+ from typing import Any , ClassVar , Generic , TypeAlias , TypeVar , overload
55from typing_extensions import Self
66
77import numpy
88from networkx .classes .coreviews import AdjacencyView , AtlasView
99from networkx .classes .digraph import DiGraph
1010from networkx .classes .reportviews import DegreeView , DiDegreeView , EdgeView , NodeView , OutEdgeView
1111
12+ _DataBound : TypeAlias = Mapping [str , Any ]
13+
1214_Node = TypeVar ("_Node" , bound = Hashable )
13- _NodeWithData : TypeAlias = tuple [_Node , dict [str , Any ]]
14- _NodePlus : TypeAlias = _Node | _NodeWithData [_Node ]
15+ _NodeData = TypeVar ("_NodeData" , bound = _DataBound , default = dict [str , Any ])
16+ _EdgeData = TypeVar ("_EdgeData" , bound = _DataBound , default = dict [str , Any ])
17+
18+ _NodeWithData : TypeAlias = tuple [_Node , _NodeData ]
19+ _NodePlus : TypeAlias = _Node | _NodeWithData [_Node , _NodeData ]
1520_Edge : TypeAlias = tuple [_Node , _Node ]
16- _EdgeWithData : TypeAlias = tuple [_Node , _Node , dict [ str , Any ] ]
17- _EdgePlus : TypeAlias = _Edge [_Node ] | _EdgeWithData [_Node ]
21+ _EdgeWithData : TypeAlias = tuple [_Node , _Node , _EdgeData ]
22+ _EdgePlus : TypeAlias = _Edge [_Node ] | _EdgeWithData [_Node , _EdgeData ]
1823_MapFactory : TypeAlias = Callable [[], MutableMapping [str , Any ]]
1924_NBunch : TypeAlias = _Node | Iterable [_Node ] | None
2025_Data : TypeAlias = (
21- Graph [_Node ]
22- | dict [_Node , dict [_Node , dict [ str , Any ] ]]
26+ Graph [_Node , _NodeData , _EdgeData ]
27+ | dict [_Node , dict [_Node , _NodeData ]]
2328 | dict [_Node , Iterable [_Node ]]
24- | Iterable [_EdgePlus [_Node ]]
29+ | Iterable [_EdgePlus [_Node , _EdgeData ]]
2530 | numpy .ndarray [Any , Any ]
2631 # | scipy.sparse.base.spmatrix
2732)
2833
2934__all__ = ["Graph" ]
3035
31- class Graph (Collection [_Node ]):
36+ class Graph (Collection [_Node ], Generic [ _Node , _NodeData , _EdgeData ] ):
3237 __networkx_backend__ : ClassVar [str ]
3338 node_dict_factory : ClassVar [_MapFactory ]
3439 node_attr_dict_factory : ClassVar [_MapFactory ]
@@ -40,14 +45,16 @@ class Graph(Collection[_Node]):
4045 graph : dict [str , Any ]
4146 __networkx_cache__ : dict [str , Any ]
4247
43- def to_directed_class (self ) -> type [DiGraph [_Node ]]: ...
44- def to_undirected_class (self ) -> type [Graph [_Node ]]: ...
48+ def to_directed_class (self ) -> type [DiGraph [_Node , _NodeData , _EdgeData ]]: ...
49+ def to_undirected_class (self ) -> type [Graph [_Node , _NodeData , _EdgeData ]]: ...
4550 # @_dispatchable adds `backend` argument, but this decorated is unsupported constructor type here
4651 # and __init__() ignores this argument
4752 def __new__ (cls , * args , backend = None , ** kwargs ) -> Self : ...
48- def __init__ (self , incoming_graph_data : _Data [_Node ] | None = None , ** attr : Any ) -> None : ... # attr: key=value pairs
53+ def __init__ (
54+ self , incoming_graph_data : _Data [_Node , _NodeData , _EdgeData ] | None = None , ** attr : Any
55+ ) -> None : ... # attr: key=value pairs
4956 @cached_property
50- def adj (self ) -> AdjacencyView [_Node , _Node , dict [ str , Any ] ]: ...
57+ def adj (self ) -> AdjacencyView [_Node , _Node , _EdgeData ]: ...
5158 # This object is a read-only dict-like structure
5259 @property
5360 def name (self ) -> str : ...
@@ -58,49 +65,53 @@ class Graph(Collection[_Node]):
5865 def __len__ (self ) -> int : ...
5966 def __getitem__ (self , n : _Node ) -> AtlasView [_Node , str , Any ]: ...
6067 def add_node (self , node_for_adding : _Node , ** attr : Any ) -> None : ... # attr: Set or change node attributes using key=value
61- def add_nodes_from (self , nodes_for_adding : Iterable [_NodePlus [_Node ]], ** attr : Any ) -> None : ... # attr: key=value pairs
68+ def add_nodes_from (
69+ self , nodes_for_adding : Iterable [_NodePlus [_Node , _NodeData ]], ** attr : Any
70+ ) -> None : ... # attr: key=value pairs
6271 def remove_node (self , n : _Node ) -> None : ...
6372 def remove_nodes_from (self , nodes : Iterable [_Node ]) -> None : ...
6473 @cached_property
65- def nodes (self ) -> NodeView [_Node ]: ...
74+ def nodes (self ) -> NodeView [_Node , _NodeData , _EdgeData ]: ...
6675 def number_of_nodes (self ) -> int : ...
6776 def order (self ) -> int : ...
6877 def has_node (self , n : _Node ) -> bool : ...
6978 # Including subtypes' possible return types for LSP
7079 def add_edge (self , u_of_edge : _Node , v_of_edge : _Node , ** attr : Any ) -> Hashable | None : ...
7180 # attr: Edge data (or labels or objects) can be assigned using keyword arguments
72- def add_edges_from (self , ebunch_to_add : Iterable [_EdgePlus [_Node ]], ** attr : Any ) -> None : ...
81+ def add_edges_from (self , ebunch_to_add : Iterable [_EdgePlus [_Node , _EdgeData ]], ** attr : Any ) -> None : ...
7382 # attr: Edge data (or labels or objects) can be assigned using keyword arguments
7483 def add_weighted_edges_from (
7584 self , ebunch_to_add : Iterable [tuple [_Node , _Node , float | Decimal | None ]], weight : str = "weight" , ** attr : Any
7685 ) -> None : ...
7786 # attr: Edge attributes to add/update for all edges.
7887 def remove_edge (self , u : _Node , v : _Node ) -> None : ...
79- def remove_edges_from (self , ebunch : Iterable [_EdgePlus [_Node ]]) -> None : ...
88+ def remove_edges_from (self , ebunch : Iterable [_EdgePlus [_Node , _EdgeData ]]) -> None : ...
8089 @overload
81- def update (self , edges : Graph [_Node ], nodes : None = None ) -> None : ...
90+ def update (self , edges : Graph [_Node , _NodeData , _EdgeData ], nodes : None = None ) -> None : ...
8291 @overload
8392 def update (
84- self , edges : Graph [_Node ] | Iterable [_EdgePlus [_Node ]] | None = None , nodes : Iterable [_Node ] | None = None
93+ self ,
94+ edges : Graph [_Node , _NodeData , _EdgeData ] | Iterable [_EdgePlus [_Node , _EdgeData ]] | None = None ,
95+ nodes : Iterable [_Node ] | None = None ,
8596 ) -> None : ...
8697 def has_edge (self , u : _Node , v : _Node ) -> bool : ...
8798 def neighbors (self , n : _Node ) -> Iterator [_Node ]: ...
8899 @cached_property
89100 # Including subtypes' possible return types for LSP
90- def edges (self ) -> EdgeView [_Node ] | OutEdgeView [_Node ]: ...
91- def get_edge_data (self , u : _Node , v : _Node , default : Any = None ) -> dict [ str , Any ] : ...
101+ def edges (self ) -> EdgeView [_Node , _NodeData , _EdgeData ] | OutEdgeView [_Node , _NodeData , _EdgeData ]: ...
102+ def get_edge_data (self , u : _Node , v : _Node , default : Any = None ) -> _EdgeData : ...
92103 # default: any Python object
93- def adjacency (self ) -> Iterator [tuple [_Node , dict [_Node , dict [ str , Any ] ]]]: ...
104+ def adjacency (self ) -> Iterator [tuple [_Node , dict [_Node , _EdgeData ]]]: ...
94105 @cached_property
95106 # Including subtypes' possible return types for LSP
96- def degree (self ) -> DegreeView [_Node ] | DiDegreeView [_Node ]: ...
107+ def degree (self ) -> DegreeView [_Node , _NodeData , _EdgeData ] | DiDegreeView [_Node , _NodeData , _EdgeData ]: ...
97108 def clear (self ) -> None : ...
98109 def clear_edges (self ) -> None : ...
99110 def is_multigraph (self ) -> bool : ...
100111 def is_directed (self ) -> bool : ...
101112 def copy (self , as_view : bool = False ) -> Self : ...
102- def to_directed (self , as_view : bool = False ) -> DiGraph [_Node ]: ...
103- def to_undirected (self , as_view : bool = False ) -> Graph [_Node ]: ...
113+ def to_directed (self , as_view : bool = False ) -> DiGraph [_Node , _NodeData , _EdgeData ]: ...
114+ def to_undirected (self , as_view : bool = False ) -> Graph [_Node , _NodeData , _EdgeData ]: ...
104115 def subgraph (self , nodes : _NBunch [_Node ]) -> Self : ...
105116 def edge_subgraph (self , edges : Iterable [_Edge [_Node ]]) -> Self : ...
106117 @overload
0 commit comments