From c08bb06385840713a3c954514b60b9f4375e0da6 Mon Sep 17 00:00:00 2001 From: Copa! Date: Sat, 17 Oct 2026 13:21:03 -0700 Subject: [PATCH 1/3] Add failing test for scalar values in networkx set_node_attributes and set_edge_attributes. --- .../@tests/test_cases/check_tricky_function_params-py312.py | 5 +++++ 1 file changed, 5 insertions(+) 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..cd72729cb0c5 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,8 @@ assert_type(degree_view(""), int) assert_type(degree_view([""]), DegreeView[str]) assert_type(degree_view({""}), DegreeView[str]) + +# Test that set_node_attributes/set_edge_attributes accept a scalar value with a name +G_path = nx.path_graph(3) +nx.set_node_attributes(G_path, 5, "some name") +nx.set_edge_attributes(G_path, 5, "some name") From 180da487927dedb8df03cfd0e0e5b4767d8510c4 Mon Sep 17 00:00:00 2001 From: Copa! Date: Sat, 17 Oct 2026 13:30:18 -0700 Subject: [PATCH 2/3] Accept scalar values in networkx set_node_attributes and set_edge_attributes when a name is given. --- stubs/networkx/networkx/classes/function.pyi | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/stubs/networkx/networkx/classes/function.pyi b/stubs/networkx/networkx/classes/function.pyi index 17d152c96157..8a7aa89483c7 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[Hashable], @@ -93,6 +92,8 @@ def set_node_attributes( **backend_kwargs, ) -> None: ... @overload +def set_node_attributes(G: Graph[Hashable], values: object, name: str, *, backend=None, **backend_kwargs) -> None: ... +@overload def set_node_attributes( G: Graph[_Node], values: SupportsItems[_Node, SupportsKeysAndGetItem[Incomplete, Incomplete] | Iterable[tuple[Incomplete, Incomplete]]], @@ -126,6 +127,10 @@ def set_edge_attributes( **backend_kwargs, ) -> None: ... @overload +def set_edge_attributes( + G: Graph[Hashable], values: object, name: str, *, backend: str | None = None, **backend_kwargs +) -> None: ... +@overload def set_edge_attributes( G: Graph[Hashable], values, name: None = None, *, backend: str | None = None, **backend_kwargs ) -> None: ... From e7601809b06861d1908e50bb5d6408d1e3edf828 Mon Sep 17 00:00:00 2001 From: Copa! Date: Sat, 17 Oct 2026 13:30:18 -0700 Subject: [PATCH 3/3] Remove the regression test since it is not needed for this simple fix. --- .../@tests/test_cases/check_tricky_function_params-py312.py | 5 ----- 1 file changed, 5 deletions(-) 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 cd72729cb0c5..20a38a85a8ad 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,8 +23,3 @@ assert_type(degree_view(""), int) assert_type(degree_view([""]), DegreeView[str]) assert_type(degree_view({""}), DegreeView[str]) - -# Test that set_node_attributes/set_edge_attributes accept a scalar value with a name -G_path = nx.path_graph(3) -nx.set_node_attributes(G_path, 5, "some name") -nx.set_edge_attributes(G_path, 5, "some name")