From 1e048d9d877c3c32b118af0038f9b7f8c8865d4e Mon Sep 17 00:00:00 2001 From: Hrithik Saini <49943287+hrithiksaini99@users.noreply.github.com> Date: Wed, 4 Nov 2026 14:36:59 +0530 Subject: [PATCH] Allow scalar values in networkx.set_edge_attributes and set_node_attributes --- .../check_tricky_function_params-py312.py | 15 ++++++++++++++ stubs/networkx/networkx/classes/function.pyi | 20 ++++++++++++------- 2 files changed, 28 insertions(+), 7 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 20a38a85a8ad..49f0a1c03fce 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,18 @@ assert_type(degree_view(""), int) assert_type(degree_view([""]), DegreeView[str]) assert_type(degree_view({""}), DegreeView[str]) + +# Test set_edge_attributes and set_node_attributes with scalar values (issue #16542) +nx.set_edge_attributes(G, 5, "weight") +nx.set_edge_attributes(G, "red", name="color") +nx.set_edge_attributes(G, {(1, 2): 10}, "capacity") +nx.set_edge_attributes(G, {(1, 2): {"weight": 10}}) + +MG: nx.MultiGraph[int] = nx.MultiGraph() +nx.set_edge_attributes(MG, 5, "weight") +nx.set_edge_attributes(MG, {(1, 2, 0): {"weight": 10}}) + +nx.set_node_attributes(G, 5, "cost") +nx.set_node_attributes(G, "red", name="color") +nx.set_node_attributes(G, {1: 10}, "cost") +nx.set_node_attributes(G, {1: {"cost": 10}}) diff --git a/stubs/networkx/networkx/classes/function.pyi b/stubs/networkx/networkx/classes/function.pyi index 8ac27209e6a0..e85463f01111 100644 --- a/stubs/networkx/networkx/classes/function.pyi +++ b/stubs/networkx/networkx/classes/function.pyi @@ -1,4 +1,4 @@ -from _typeshed import Incomplete, SupportsItems, SupportsKeysAndGetItem, Unused +from _typeshed import Incomplete, SupportsItems, SupportsKeysAndGetItem from collections.abc import Callable, Collection, Generator, Iterable, Iterator from typing import Literal, TypeVar, overload @@ -82,11 +82,10 @@ 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], - values: SupportsItems[_Node, Unused], + values: object, name: str, *, backend=None, # @_dispatchable adds these arguments, but we can't use this decorator with @overload @@ -110,7 +109,7 @@ def remove_node_attributes(G: Graph[_Node], *attr_names, nbunch=None) -> None: . @overload def set_edge_attributes( G: Graph[_Node], - values: SupportsItems[tuple[_Node, _Node], Incomplete], + values: object, name: str, *, backend: str | None = None, # @_dispatchable adds these arguments, but we can't use this decorator with @overload @@ -119,14 +118,21 @@ def set_edge_attributes( @overload def set_edge_attributes( G: MultiGraph[_Node], - values: dict[tuple[_Node, _Node, Incomplete], Incomplete], - name: str, + values: SupportsItems[tuple[_Node, _Node, Incomplete], Incomplete], + name: None = None, *, 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: ... +def set_edge_attributes( + G: Graph[_Node], + values: SupportsItems[tuple[_Node, _Node], Incomplete], + name: None = None, + *, + backend: str | None = None, + **backend_kwargs, +) -> None: ... @_dispatchable def get_edge_attributes(G: Graph[_Node], name: str, default=None) -> dict[tuple[_Node, _Node], Incomplete]: ...