From 8ded76c1bdc0c62d4d95e4bb76f70f3567273025 Mon Sep 17 00:00:00 2001 From: Fire162 Date: Sat, 24 Oct 2026 19:12:56 +0530 Subject: [PATCH 1/3] [networkx] Allow scalar values with name in set_edge_attributes and set_node_attributes --- .../@tests/test_cases/check_tricky_function_params-py312.py | 6 ++++++ stubs/networkx/networkx/classes/function.pyi | 5 ++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/stubs/networkx/@tests/test_cases/check_tricky_function_params-py312.py b/stubs/networkx/@tests/test_cases/check_tricky_function_params-py312.py index 20a38a85a8ad..1e040f031aac 100644 --- a/stubs/networkx/@tests/test_cases/check_tricky_function_params-py312.py +++ b/stubs/networkx/@tests/test_cases/check_tricky_function_params-py312.py @@ -23,3 +23,9 @@ assert_type(degree_view(""), int) assert_type(degree_view([""]), DegreeView[str]) assert_type(degree_view({""}), DegreeView[str]) + +# Test scalar attributes with name in set_node_attributes and set_edge_attributes +nx.set_node_attributes(G, 5, "some_name") +nx.set_node_attributes(G, {1: "val"}, "some_name") +nx.set_edge_attributes(G, 10, "weight") +nx.set_edge_attributes(G, {(1, 2): 10}, "weight") diff --git a/stubs/networkx/networkx/classes/function.pyi b/stubs/networkx/networkx/classes/function.pyi index 8ac27209e6a0..eab1fdb0ad14 100644 --- a/stubs/networkx/networkx/classes/function.pyi +++ b/stubs/networkx/networkx/classes/function.pyi @@ -82,7 +82,6 @@ def to_directed(graph): ... def to_undirected(graph): ... def create_empty_copy(G: Graph[_Node], with_data: bool = True): ... -# incomplete: Can "Any scalar value" be enforced? @overload def set_node_attributes( G: Graph[_Node], @@ -101,6 +100,8 @@ def set_node_attributes( backend=None, **backend_kwargs, ) -> None: ... +@overload +def set_node_attributes(G: Graph[_Node], values: object, name: str, *, backend=None, **backend_kwargs) -> None: ... @_dispatchable def get_node_attributes(G: Graph[_Node], name: str, default=None) -> dict[_Node, Incomplete]: ... @@ -126,6 +127,8 @@ def set_edge_attributes( **backend_kwargs, ) -> None: ... @overload +def set_edge_attributes(G: Graph[_Node], values: object, name: str, *, backend: str | None = None, **backend_kwargs) -> None: ... +@overload def set_edge_attributes(G: Graph[_Node], values, name: None = None, *, backend: str | None = None, **backend_kwargs) -> None: ... @_dispatchable From 3af783db353d883e7f76c0f1926670fa25b2be35 Mon Sep 17 00:00:00 2001 From: Fire162 Date: Mon, 26 Oct 2026 02:57:09 +0530 Subject: [PATCH 2/3] [networkx] Add type annotations for backend and kwargs in set_*_attributes --- stubs/networkx/networkx/classes/function.pyi | 26 ++++++++++++-------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/stubs/networkx/networkx/classes/function.pyi b/stubs/networkx/networkx/classes/function.pyi index eab1fdb0ad14..3a1724afc3a2 100644 --- a/stubs/networkx/networkx/classes/function.pyi +++ b/stubs/networkx/networkx/classes/function.pyi @@ -1,6 +1,6 @@ from _typeshed import Incomplete, SupportsItems, SupportsKeysAndGetItem, Unused from collections.abc import Callable, Collection, Generator, Iterable, Iterator -from typing import Literal, TypeVar, overload +from typing import Any, Literal, TypeVar, overload from networkx import _dispatchable from networkx.algorithms.planarity import PlanarEmbedding @@ -88,8 +88,8 @@ def set_node_attributes( values: SupportsItems[_Node, Unused], name: str, *, - backend=None, # @_dispatchable adds these arguments, but we can't use this decorator with @overload - **backend_kwargs, + backend: str | None = None, # @_dispatchable adds these arguments, but we can't use this decorator with @overload + **backend_kwargs: Any, ) -> None: ... @overload def set_node_attributes( @@ -97,11 +97,13 @@ def set_node_attributes( values: SupportsItems[_Node, SupportsKeysAndGetItem[Incomplete, Incomplete] | Iterable[tuple[Incomplete, Incomplete]]], name: None = None, *, - backend=None, - **backend_kwargs, + backend: str | None = None, + **backend_kwargs: Any, ) -> None: ... @overload -def set_node_attributes(G: Graph[_Node], values: object, name: str, *, backend=None, **backend_kwargs) -> None: ... +def set_node_attributes( + G: Graph[_Node], values: object, name: str, *, backend: str | None = None, **backend_kwargs: Any +) -> None: ... @_dispatchable def get_node_attributes(G: Graph[_Node], name: str, default=None) -> dict[_Node, Incomplete]: ... @@ -115,7 +117,7 @@ def set_edge_attributes( name: str, *, backend: str | None = None, # @_dispatchable adds these arguments, but we can't use this decorator with @overload - **backend_kwargs, + **backend_kwargs: Any, ) -> None: ... @overload def set_edge_attributes( @@ -124,12 +126,16 @@ def set_edge_attributes( name: str, *, backend: str | None = None, - **backend_kwargs, + **backend_kwargs: Any, ) -> None: ... @overload -def set_edge_attributes(G: Graph[_Node], values: object, name: str, *, backend: str | None = None, **backend_kwargs) -> None: ... +def set_edge_attributes( + G: Graph[_Node], values: object, name: str, *, backend: str | None = None, **backend_kwargs: Any +) -> None: ... @overload -def set_edge_attributes(G: Graph[_Node], values, name: None = None, *, backend: str | None = None, **backend_kwargs) -> None: ... +def set_edge_attributes( + G: Graph[_Node], values: Incomplete, name: None = None, *, backend: str | None = None, **backend_kwargs: Any +) -> None: ... @_dispatchable def get_edge_attributes(G: Graph[_Node], name: str, default=None) -> dict[tuple[_Node, _Node], Incomplete]: ... From 15b19575926a310ff5ef905bd1a9e928fff3b5c7 Mon Sep 17 00:00:00 2001 From: Abhinav Maurya Date: Mon, 26 Oct 2026 04:00:19 +0530 Subject: [PATCH 3/3] [networkx] Use Any instead of Incomplete for values in set_edge_attributes overload --- stubs/networkx/networkx/classes/function.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stubs/networkx/networkx/classes/function.pyi b/stubs/networkx/networkx/classes/function.pyi index 3a1724afc3a2..330b799b1d6b 100644 --- a/stubs/networkx/networkx/classes/function.pyi +++ b/stubs/networkx/networkx/classes/function.pyi @@ -134,7 +134,7 @@ def set_edge_attributes( ) -> None: ... @overload def set_edge_attributes( - G: Graph[_Node], values: Incomplete, name: None = None, *, backend: str | None = None, **backend_kwargs: Any + G: Graph[_Node], values: Any, name: None = None, *, backend: str | None = None, **backend_kwargs: Any ) -> None: ... @_dispatchable