Skip to content

Commit fca7c95

Browse files
authored
Merge branch 'main' into networkx-incorrect-SupportsGetItem
2 parents 3976eb0 + 9fecaca commit fca7c95

File tree

5 files changed

+64
-38
lines changed

5 files changed

+64
-38
lines changed

stubs/networkx/networkx/algorithms/planarity.pyi

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
from _typeshed import Incomplete
2-
from collections.abc import Generator, Mapping, MutableSet, Reversible
1+
from _typeshed import Incomplete, Unused
2+
from collections.abc import Generator, Iterable, Mapping, MutableSet, Reversible
3+
from typing import NoReturn
34

45
from networkx.classes.digraph import DiGraph
5-
from networkx.classes.graph import Graph, _Node
6+
from networkx.classes.graph import Graph, _EdgePlus, _Node
67
from networkx.utils.backends import _dispatchable
78

89
__all__ = ["check_planarity", "is_planar", "PlanarEmbedding"]
@@ -87,6 +88,9 @@ class LRPlanarity:
8788
def sign(self, e): ...
8889
def sign_recursive(self, e): ...
8990

91+
# NOTE: Graph subclasses relationships are so complex
92+
# we're only overriding methods that differ in signature from the base classes
93+
# to use inheritance to our advantage and reduce complexity
9094
class PlanarEmbedding(DiGraph[_Node]):
9195
def get_data(self) -> dict[_Node, list[_Node]]: ...
9296
def set_data(self, data: Mapping[_Node, Reversible[_Node]]) -> None: ...
@@ -101,4 +105,9 @@ class PlanarEmbedding(DiGraph[_Node]):
101105
def traverse_face(
102106
self, v: _Node, w: _Node, mark_half_edges: MutableSet[tuple[_Node, _Node]] | None = None
103107
) -> list[_Node]: ...
104-
def to_undirected(self, reciprocal: bool = False, as_view: bool = False) -> Graph[_Node]: ... # type: ignore[override]
108+
# Overriden in __init__ to always raise
109+
def add_edge(self, u_of_edge: _Node, v_of_edge: _Node, **attr: Unused) -> NoReturn: ...
110+
def add_edges_from(self, ebunch_to_add: Iterable[_EdgePlus[_Node]], **attr: Unused) -> NoReturn: ...
111+
def add_weighted_edges_from(
112+
self, ebunch_to_add: Iterable[tuple[_Node, _Node, float]], weight: str = "weight", **attr: Unused
113+
) -> NoReturn: ...

stubs/networkx/networkx/classes/digraph.pyi

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@ from networkx.classes.reportviews import (
1010
InDegreeView,
1111
InEdgeView,
1212
InMultiDegreeView,
13+
InMultiEdgeView,
1314
OutDegreeView,
1415
OutEdgeView,
1516
OutMultiDegreeView,
1617
)
1718

1819
__all__ = ["DiGraph"]
1920

21+
# NOTE: Graph subclasses relationships are so complex
22+
# we're only overriding methods that differ in signature from the base classes
23+
# to use inheritance to our advantage and reduce complexity
2024
class DiGraph(Graph[_Node]):
2125
@cached_property
2226
def succ(self) -> AdjacencyView[_Node, _Node, dict[str, Any]]: ...
@@ -30,17 +34,19 @@ class DiGraph(Graph[_Node]):
3034

3135
def predecessors(self, n: _Node) -> Iterator[_Node]: ...
3236
@cached_property
37+
def edges(self) -> OutEdgeView[_Node]: ...
38+
@cached_property
3339
def out_edges(self) -> OutEdgeView[_Node]: ...
3440
@cached_property
35-
def in_edges(self) -> InEdgeView[_Node]: ...
41+
# Including subtypes' possible return types for LSP
42+
def in_edges(self) -> InEdgeView[_Node] | InMultiEdgeView[_Node]: ...
43+
@cached_property
44+
def degree(self) -> DiDegreeView[_Node]: ...
3645
@cached_property
46+
# Including subtypes' possible return types for LSP
3747
def in_degree(self) -> InDegreeView[_Node] | InMultiDegreeView[_Node]: ...
3848
@cached_property
49+
# Including subtypes' possible return types for LSP
3950
def out_degree(self) -> OutDegreeView[_Node] | OutMultiDegreeView[_Node]: ...
40-
def to_undirected(self, reciprocal: bool = False, as_view: bool = False) -> Graph[_Node]: ... # type: ignore[override]
41-
# reciprocal : If True, only edges that appear in both directions ... will be kept in the undirected graph.
51+
def to_undirected(self, reciprocal: bool = False, as_view: bool = False) -> Graph[_Node]: ... # type: ignore[override] # Has an additional `reciprocal` keyword argument
4252
def reverse(self, copy: bool = True) -> Self: ...
43-
@cached_property
44-
def edges(self) -> OutEdgeView[_Node]: ... # type: ignore[override] # An OutEdgeView of the DiGraph as G.edges or G.edges().
45-
@cached_property
46-
def degree(self) -> int | DiDegreeView[_Node]: ... # type: ignore[override] # Returns DiDegreeView or int

stubs/networkx/networkx/classes/graph.pyi

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ from typing_extensions import Self, TypeAlias
66
import numpy
77
from networkx.classes.coreviews import AdjacencyView, AtlasView
88
from networkx.classes.digraph import DiGraph
9-
from networkx.classes.reportviews import DegreeView, EdgeView, NodeView
9+
from networkx.classes.reportviews import DegreeView, DiDegreeView, EdgeView, NodeView, OutEdgeView
1010

1111
_Node = TypeVar("_Node", bound=Hashable)
1212
_NodeWithData: TypeAlias = tuple[_Node, dict[str, Any]]
@@ -49,10 +49,10 @@ class Graph(Collection[_Node]):
4949
def name(self) -> str: ...
5050
@name.setter
5151
def name(self, s: str) -> None: ...
52-
def __getitem__(self, n: _Node) -> AtlasView[_Node, str, Any]: ...
5352
def __iter__(self) -> Iterator[_Node]: ...
5453
def __contains__(self, n: object) -> bool: ...
5554
def __len__(self) -> int: ...
55+
def __getitem__(self, n: _Node) -> AtlasView[_Node, str, Any]: ...
5656
def add_node(self, node_for_adding: _Node, **attr: Any) -> None: ... # attr: Set or change node attributes using key=value
5757
def add_nodes_from(self, nodes_for_adding: Iterable[_NodePlus[_Node]], **attr: Any) -> None: ... # attr: key=value pairs
5858
def remove_node(self, n: _Node) -> None: ...
@@ -62,7 +62,8 @@ class Graph(Collection[_Node]):
6262
def number_of_nodes(self) -> int: ...
6363
def order(self) -> int: ...
6464
def has_node(self, n: _Node) -> bool: ...
65-
def add_edge(self, u_of_edge: _Node, v_of_edge: _Node, **attr: Any) -> None: ...
65+
# Including subtypes' possible return types for LSP
66+
def add_edge(self, u_of_edge: _Node, v_of_edge: _Node, **attr: Any) -> Hashable | None: ...
6667
# attr: Edge data (or labels or objects) can be assigned using keyword arguments
6768
def add_edges_from(self, ebunch_to_add: Iterable[_EdgePlus[_Node]], **attr: Any) -> None: ...
6869
# attr: Edge data (or labels or objects) can be assigned using keyword arguments
@@ -81,20 +82,22 @@ class Graph(Collection[_Node]):
8182
def has_edge(self, u: _Node, v: _Node) -> bool: ...
8283
def neighbors(self, n: _Node) -> Iterator[_Node]: ...
8384
@cached_property
84-
def edges(self) -> EdgeView[_Node]: ...
85+
# Including subtypes' possible return types for LSP
86+
def edges(self) -> EdgeView[_Node] | OutEdgeView[_Node]: ...
8587
def get_edge_data(self, u: _Node, v: _Node, default: Any = None) -> dict[str, Any]: ...
8688
# default: any Python object
8789
def adjacency(self) -> Iterator[tuple[_Node, dict[_Node, dict[str, Any]]]]: ...
8890
@cached_property
89-
def degree(self) -> int | DegreeView[_Node]: ...
91+
# Including subtypes' possible return types for LSP
92+
def degree(self) -> DegreeView[_Node] | DiDegreeView[_Node]: ...
9093
def clear(self) -> None: ...
9194
def clear_edges(self) -> None: ...
9295
def is_multigraph(self) -> bool: ...
9396
def is_directed(self) -> bool: ...
9497
def copy(self, as_view: bool = False) -> Self: ...
9598
def to_directed(self, as_view: bool = False) -> DiGraph[_Node]: ...
9699
def to_undirected(self, as_view: bool = False) -> Graph[_Node]: ...
97-
def subgraph(self, nodes: Iterable[_Node]) -> Graph[_Node]: ...
100+
def subgraph(self, nodes: _NBunch[_Node]) -> Graph[_Node]: ...
98101
def edge_subgraph(self, edges: Iterable[_Edge[_Node]]) -> Graph[_Node]: ...
99102
@overload
100103
def size(self, weight: None = None) -> int: ...

stubs/networkx/networkx/classes/multidigraph.pyi

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,36 @@ from typing import Any
33

44
from networkx.classes.coreviews import MultiAdjacencyView
55
from networkx.classes.digraph import DiGraph
6-
from networkx.classes.graph import _EdgeWithData, _Node
6+
from networkx.classes.graph import _Node
77
from networkx.classes.multigraph import MultiGraph
88
from networkx.classes.reportviews import (
9+
DiMultiDegreeView,
910
InMultiDegreeView,
10-
InMultiEdgeDataView,
1111
InMultiEdgeView,
1212
OutMultiDegreeView,
1313
OutMultiEdgeView,
1414
)
1515

1616
__all__ = ["MultiDiGraph"]
1717

18+
# NOTE: Graph subclasses relationships are so complex
19+
# we're only overriding methods that differ in signature from the base classes
20+
# to use inheritance to our advantage and reduce complexity
1821
class MultiDiGraph(MultiGraph[_Node], DiGraph[_Node]):
1922
@cached_property
2023
def succ(self) -> MultiAdjacencyView[_Node, _Node, dict[str, Any]]: ...
2124
@cached_property
2225
def pred(self) -> MultiAdjacencyView[_Node, _Node, dict[str, Any]]: ...
2326
@cached_property
24-
def edges(self) -> OutMultiEdgeView[_Node]: ... # type: ignore[override]
25-
# Returns: OutMultiEdgeView
27+
def edges(self) -> OutMultiEdgeView[_Node]: ...
2628
@cached_property
2729
def out_edges(self) -> OutMultiEdgeView[_Node]: ...
2830
@cached_property
29-
def in_edges(self) -> InMultiEdgeView[_Node] | InMultiEdgeDataView[_Node, _EdgeWithData[_Node]]: ... # type: ignore[override]
30-
# Returns : InMultiEdgeView or InMultiEdgeDataView
31+
def in_edges(self) -> InMultiEdgeView[_Node]: ...
32+
@cached_property
33+
def degree(self) -> DiMultiDegreeView[_Node]: ...
3134
@cached_property
3235
def in_degree(self) -> InMultiDegreeView[_Node]: ...
3336
@cached_property
3437
def out_degree(self) -> OutMultiDegreeView[_Node]: ...
35-
def to_undirected(self, reciprocal: bool = False, as_view: bool = False) -> MultiGraph[_Node]: ... # type: ignore[override]
36-
def reverse(self, copy: bool = True) -> MultiDiGraph[_Node]: ...
37-
def copy(self, as_view: bool = False) -> MultiDiGraph[_Node]: ...
38+
def to_undirected(self, reciprocal: bool = False, as_view: bool = False) -> MultiGraph[_Node]: ... # type: ignore[override] # Has an additional `reciprocal` keyword argument

stubs/networkx/networkx/classes/multigraph.pyi

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ from typing_extensions import TypeAlias, TypeVar
66
from networkx.classes.coreviews import MultiAdjacencyView
77
from networkx.classes.graph import Graph, _MapFactory, _Node
88
from networkx.classes.multidigraph import MultiDiGraph
9-
from networkx.classes.reportviews import MultiEdgeView
9+
from networkx.classes.reportviews import DiMultiDegreeView, MultiDegreeView, MultiEdgeView, OutMultiEdgeView
1010

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

@@ -15,36 +15,43 @@ _KeyT = TypeVar("_KeyT", bound=Hashable)
1515

1616
__all__ = ["MultiGraph"]
1717

18+
# NOTE: Graph subclasses relationships are so complex
19+
# we're only overriding methods that differ in signature from the base classes
20+
# to use inheritance to our advantage and reduce complexity
1821
class MultiGraph(Graph[_Node]):
1922
edge_key_dict_factory: ClassVar[_MapFactory]
23+
def to_directed_class(self) -> type[MultiDiGraph[_Node]]: ...
24+
def to_undirected_class(self) -> type[MultiGraph[_Node]]: ...
2025
def __init__(self, incoming_graph_data=None, multigraph_input: bool | None = None, **attr: Any) -> None: ...
2126
@cached_property
2227
def adj(self) -> MultiAdjacencyView[_Node, _Node, dict[str, Any]]: ... # data can be any type
2328
def new_edge_key(self, u: _Node, v: _Node) -> int: ...
24-
@overload # type: ignore[override] # Has an additional `key` keyword argument
29+
# key : hashable identifier, optional (default=lowest unused integer)
30+
@overload # type: ignore[override] # More complex overload
2531
def add_edge(self, u_for_edge: _Node, v_for_edge: _Node, key: int | None = None, **attr: Any) -> int: ...
2632
@overload
2733
def add_edge(self, u_for_edge: _Node, v_for_edge: _Node, key: _KeyT, **attr: Any) -> _KeyT: ...
28-
# key : hashable identifier, optional (default=lowest unused integer)
2934
def remove_edge(self, u: _Node, v: _Node, key: Hashable | None = None) -> None: ...
3035
def has_edge(self, u: _Node, v: _Node, key: Hashable | None = None) -> bool: ...
36+
@cached_property
37+
# Including subtypes' possible return types for LSP
38+
def edges(self) -> MultiEdgeView[_Node] | OutMultiEdgeView[_Node]: ...
39+
# key : hashable identifier, optional (default=None).
40+
# default : any Python object (default=None). Value to return if the specific edge (u, v, key) is not found.
41+
# Returns: The edge attribute dictionary.
3142
@overload # type: ignore[override]
3243
def get_edge_data(
3344
self, u: _Node, v: _Node, key: Hashable, default: _DefaultT | None = None
3445
) -> dict[str, Any] | _DefaultT: ...
35-
# key : hashable identifier, optional (default=None).
36-
# default : any Python object (default=None). Value to return if the specific edge (u, v, key) is not found.
37-
# Returns: The edge attribute dictionary.
46+
# default : any Python object (default=None). Value to return if there are no edges between u and v and no key is specified.
47+
# Returns: A dictionary mapping edge keys to attribute dictionaries for each of those edges if no specific key is provided.
3848
@overload
3949
def get_edge_data(
4050
self, u: _Node, v: _Node, key: None = None, default: _DefaultT | None = None
4151
) -> dict[Hashable, dict[str, Any] | _DefaultT]: ...
42-
# default : any Python object (default=None). Value to return if there are no edges between u and v and no key is specified.
43-
# Returns: A dictionary mapping edge keys to attribute dictionaries for each of those edges if no specific key is provided.
4452
def copy(self, as_view: bool = False) -> MultiGraph[_Node]: ...
53+
@cached_property
54+
# Including subtypes' possible return types for LSP
55+
def degree(self) -> MultiDegreeView[_Node] | DiMultiDegreeView[_Node]: ...
4556
def to_directed(self, as_view: bool = False) -> MultiDiGraph[_Node]: ...
4657
def to_undirected(self, as_view: bool = False) -> MultiGraph[_Node]: ...
47-
def number_of_edges(self, u: _Node | None = None, v: _Node | None = None) -> int: ...
48-
@cached_property
49-
def edges(self) -> MultiEdgeView[_Node]: ... # type: ignore[override]
50-
# Returns: MultiEdgeView

0 commit comments

Comments
 (0)