From cd2e2c478de7458ae5b9c41f2e0f092433367ad8 Mon Sep 17 00:00:00 2001 From: Sam Grayson Date: Tue, 19 Aug 2025 15:43:07 -0500 Subject: [PATCH 1/2] Correct type annotations on NetworkX NodeViews --- stubs/networkx/networkx/classes/digraph.pyi | 4 ++-- stubs/networkx/networkx/classes/reportviews.pyi | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/stubs/networkx/networkx/classes/digraph.pyi b/stubs/networkx/networkx/classes/digraph.pyi index 27596d2600ef..482c2c208c5f 100644 --- a/stubs/networkx/networkx/classes/digraph.pyi +++ b/stubs/networkx/networkx/classes/digraph.pyi @@ -34,9 +34,9 @@ class DiGraph(Graph[_Node]): @cached_property def in_edges(self) -> InEdgeView[_Node]: ... @cached_property - def in_degree(self) -> int | InDegreeView[_Node] | InMultiDegreeView[_Node]: ... + def in_degree(self) -> InDegreeView[_Node] | InMultiDegreeView[_Node]: ... @cached_property - def out_degree(self) -> int | OutDegreeView[_Node] | OutMultiDegreeView[_Node]: ... + def out_degree(self) -> OutDegreeView[_Node] | OutMultiDegreeView[_Node]: ... def to_undirected(self, reciprocal: bool = False, as_view: bool = False) -> Graph[_Node]: ... # type: ignore[override] # reciprocal : If True, only edges that appear in both directions ... will be kept in the undirected graph. def reverse(self, copy: bool = True) -> Self: ... diff --git a/stubs/networkx/networkx/classes/reportviews.pyi b/stubs/networkx/networkx/classes/reportviews.pyi index ff9078493a19..344cb757e5f5 100644 --- a/stubs/networkx/networkx/classes/reportviews.pyi +++ b/stubs/networkx/networkx/classes/reportviews.pyi @@ -41,9 +41,9 @@ class NodeView(Mapping[_Node, dict[str, Any]], AbstractSet[_Node]): def __getitem__(self, n: _Node) -> dict[str, Any]: ... def __contains__(self, n: object) -> bool: ... @overload - def __call__(self, data: Literal[False] = False, default=None) -> Iterator[_Node]: ... + def __call__(self, data: Literal[False] = False, default=None) -> NodeView[_Node]: ... @overload - def __call__(self, data: Literal[True] | str, default=None) -> Iterator[tuple[_Node, dict[str, Any]]]: ... + def __call__(self, data: Literal[True] | str, default=None) -> NodeDataView[_Node]: ... def data(self, data: bool | str = True, default=None) -> NodeDataView[_Node]: ... class NodeDataView(AbstractSet[_Node]): From dbf15565a32d8c8e01ac6ee73c85e1675166ec9e Mon Sep 17 00:00:00 2001 From: Samuel Grayson Date: Wed, 20 Aug 2025 12:35:22 -0500 Subject: [PATCH 2/2] Make types more precise --- stubs/networkx/networkx/classes/reportviews.pyi | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/stubs/networkx/networkx/classes/reportviews.pyi b/stubs/networkx/networkx/classes/reportviews.pyi index 344cb757e5f5..0f5b8655b190 100644 --- a/stubs/networkx/networkx/classes/reportviews.pyi +++ b/stubs/networkx/networkx/classes/reportviews.pyi @@ -41,10 +41,10 @@ class NodeView(Mapping[_Node, dict[str, Any]], AbstractSet[_Node]): def __getitem__(self, n: _Node) -> dict[str, Any]: ... def __contains__(self, n: object) -> bool: ... @overload - def __call__(self, data: Literal[False] = False, default=None) -> NodeView[_Node]: ... + def __call__(self, data: Literal[False] = False, default=None) -> Self: ... @overload - def __call__(self, data: Literal[True] | str, default=None) -> NodeDataView[_Node]: ... - def data(self, data: bool | str = True, default=None) -> NodeDataView[_Node]: ... + def __call__(self, data: Literal[True] | str, default=None) -> Self: ... + def data(self, data: bool | str = True, default=None) -> Self: ... class NodeDataView(AbstractSet[_Node]): def __init__(self, nodedict: Mapping[str, Incomplete], data: bool | str = False, default=None) -> None: ... @@ -55,7 +55,10 @@ class NodeDataView(AbstractSet[_Node]): class DiDegreeView(Generic[_Node]): def __init__(self, G: Graph[_Node], nbunch: _NBunch[_Node] = None, weight: None | bool | str = None) -> None: ... - def __call__(self, nbunch: _NBunch[_Node] = None, weight: None | bool | str = None) -> int | DiDegreeView[_Node]: ... + @overload + def __call__(self, nbunch: None = None, weight: None | bool | str = None) -> int: ... # type: ignore[overload-overlap] + @overload + def __call__(self, nbunch: None | Iterable[_Node], weight: None | bool | str = None) -> Self: ... def __getitem__(self, n: _Node) -> float: ... def __iter__(self) -> Iterator[tuple[_Node, float]]: ... def __len__(self) -> int: ...