Skip to content

Commit 897abe0

Browse files
authored
networkx: improve the multigraph module (#14506)
1 parent d876dba commit 897abe0

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

stubs/networkx/networkx/classes/graph.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from collections.abc import Callable, Collection, Hashable, Iterable, Iterator, Mapping, MutableMapping
1+
from collections.abc import Callable, Collection, Hashable, Iterable, Iterator, MutableMapping
22
from functools import cached_property
33
from typing import Any, ClassVar, TypeVar, overload
44
from typing_extensions import Self, TypeAlias
@@ -43,7 +43,7 @@ class Graph(Collection[_Node]):
4343
def to_undirected_class(self) -> type[Graph[_Node]]: ...
4444
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, Mapping[str, Any]]: ...
46+
def adj(self) -> AdjacencyView[_Node, _Node, dict[str, Any]]: ...
4747
# This object is a read-only dict-like structure
4848
@property
4949
def name(self) -> str: ...
@@ -82,9 +82,9 @@ class Graph(Collection[_Node]):
8282
def neighbors(self, n: _Node) -> Iterator[_Node]: ...
8383
@cached_property
8484
def edges(self) -> EdgeView[_Node]: ...
85-
def get_edge_data(self, u: _Node, v: _Node, default: Any = None) -> Mapping[str, Any]: ...
85+
def get_edge_data(self, u: _Node, v: _Node, default: Any = None) -> dict[str, Any]: ...
8686
# default: any Python object
87-
def adjacency(self) -> Iterator[tuple[_Node, Mapping[_Node, Mapping[str, Any]]]]: ...
87+
def adjacency(self) -> Iterator[tuple[_Node, dict[_Node, dict[str, Any]]]]: ...
8888
@cached_property
8989
def degree(self) -> int | DegreeView[_Node]: ...
9090
def clear(self) -> None: ...

stubs/networkx/networkx/classes/multigraph.pyi

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from collections.abc import Hashable, Mapping
1+
from collections.abc import Hashable
22
from functools import cached_property
33
from typing import Any, ClassVar, overload
44
from typing_extensions import TypeAlias, TypeVar
@@ -10,31 +10,35 @@ from networkx.classes.reportviews import MultiEdgeView
1010

1111
_MultiEdge: TypeAlias = tuple[_Node, _Node, int] # noqa: Y047
1212

13-
_Any = TypeVar("_Any")
13+
_DefaultT = TypeVar("_DefaultT")
14+
_KeyT = TypeVar("_KeyT", bound=Hashable)
1415

1516
__all__ = ["MultiGraph"]
1617

1718
class MultiGraph(Graph[_Node]):
1819
edge_key_dict_factory: ClassVar[_MapFactory]
1920
def __init__(self, incoming_graph_data=None, multigraph_input: bool | None = None, **attr: Any) -> None: ...
2021
@cached_property
21-
def adj(self) -> MultiAdjacencyView[_Node, _Node, Mapping[str, Any]]: ...
22+
def adj(self) -> MultiAdjacencyView[_Node, _Node, dict[str, Any]]: ... # data can be any type
2223
def new_edge_key(self, u: _Node, v: _Node) -> int: ...
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: ...
24+
@overload # type: ignore[override] # Has an additional `key` keyword argument
25+
def add_edge(self, u_for_edge: _Node, v_for_edge: _Node, key: int | None = None, **attr: Any) -> int: ...
26+
@overload
27+
def add_edge(self, u_for_edge: _Node, v_for_edge: _Node, key: _KeyT, **attr: Any) -> _KeyT: ...
2628
# key : hashable identifier, optional (default=lowest unused integer)
27-
def remove_edge(self, u, v, key=None): ...
28-
def has_edge(self, u: _Node, v: _Node, key=None) -> bool: ...
29+
def remove_edge(self, u: _Node, v: _Node, key: Hashable | None = None) -> None: ...
30+
def has_edge(self, u: _Node, v: _Node, key: Hashable | None = None) -> bool: ...
2931
@overload # type: ignore[override]
30-
def get_edge_data(self, u: _Node, v: _Node, key: Hashable, default: _Any | None = None) -> Mapping[str, Any] | _Any: ...
32+
def get_edge_data(
33+
self, u: _Node, v: _Node, key: Hashable, default: _DefaultT | None = None
34+
) -> dict[str, Any] | _DefaultT: ...
3135
# key : hashable identifier, optional (default=None).
3236
# default : any Python object (default=None). Value to return if the specific edge (u, v, key) is not found.
3337
# Returns: The edge attribute dictionary.
3438
@overload
3539
def get_edge_data(
36-
self, u: _Node, v: _Node, key: None = None, default: _Any | None = None
37-
) -> Mapping[Hashable, Mapping[str, Any] | _Any]: ...
40+
self, u: _Node, v: _Node, key: None = None, default: _DefaultT | None = None
41+
) -> dict[Hashable, dict[str, Any] | _DefaultT]: ...
3842
# default : any Python object (default=None). Value to return if there are no edges between u and v and no key is specified.
3943
# Returns: A dictionary mapping edge keys to attribute dictionaries for each of those edges if no specific key is provided.
4044
def copy(self, as_view: bool = False) -> MultiGraph[_Node]: ...

0 commit comments

Comments
 (0)