From 3d98d2e989020957df1771099f22db802b1edb3d Mon Sep 17 00:00:00 2001 From: Hrithik Saini <49943287+hrithiksaini99@users.noreply.github.com> Date: Wed, 4 Nov 2026 01:00:18 +0530 Subject: [PATCH 1/3] [networkx] Allow scalar values with attribute name in set_edge_attributes and set_node_attributes --- .../check_tricky_function_params-py312.py | 6 ++++++ stubs/networkx/networkx/classes/function.pyi | 18 +++++++++++------- 2 files changed, 17 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..d7aa3e7bf88c 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 set_edge_attributes and set_node_attributes with scalar value + name +nx.set_edge_attributes(G, 5, "weight") +nx.set_edge_attributes(G, "blue", "color") +nx.set_node_attributes(G, 5, "weight") +nx.set_node_attributes(G, "red", "color") diff --git a/stubs/networkx/networkx/classes/function.pyi b/stubs/networkx/networkx/classes/function.pyi index 8ac27209e6a0..ce3539da6275 100644 --- a/stubs/networkx/networkx/classes/function.pyi +++ b/stubs/networkx/networkx/classes/function.pyi @@ -82,7 +82,7 @@ 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? +# Values can be a dict-like mapping or any scalar value when name is provided @overload def set_node_attributes( G: Graph[_Node], @@ -93,6 +93,8 @@ def set_node_attributes( **backend_kwargs, ) -> None: ... @overload +def set_node_attributes(G: Graph[_Node], 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]]], @@ -109,23 +111,25 @@ 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], + G: MultiGraph[_Node], + values: dict[tuple[_Node, _Node, Incomplete], Incomplete], name: str, *, - backend: str | None = None, # @_dispatchable adds these arguments, but we can't use this decorator with @overload + backend: str | None = None, **backend_kwargs, ) -> None: ... @overload def set_edge_attributes( - G: MultiGraph[_Node], - values: dict[tuple[_Node, _Node, Incomplete], Incomplete], + G: Graph[_Node], + values: SupportsItems[tuple[_Node, _Node], Incomplete], name: str, *, - backend: str | None = None, + backend: str | None = None, # @_dispatchable adds these arguments, but we can't use this decorator with @overload **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 57e7cd9a2c6b8620d1f5b5a5e4e57198c31acb61 Mon Sep 17 00:00:00 2001 From: Hrithik Saini <49943287+hrithiksaini99@users.noreply.github.com> Date: Wed, 4 Nov 2026 11:32:15 +0530 Subject: [PATCH 2/3] [networkx] Add explicit types for backend kwargs and edge values --- stubs/networkx/networkx/classes/function.pyi | 36 ++++++++++++++------ 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/stubs/networkx/networkx/classes/function.pyi b/stubs/networkx/networkx/classes/function.pyi index ce3539da6275..4a7956a9fb04 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 @@ -89,19 +89,21 @@ 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(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: ... @overload def set_node_attributes( G: Graph[_Node], 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: ... @_dispatchable @@ -116,7 +118,7 @@ def set_edge_attributes( name: str, *, backend: str | None = None, - **backend_kwargs, + **backend_kwargs: Any, ) -> None: ... @overload def set_edge_attributes( @@ -125,12 +127,26 @@ 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(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: ( + SupportsItems[ + tuple[_Node, _Node], SupportsKeysAndGetItem[Incomplete, Incomplete] | Iterable[tuple[Incomplete, Incomplete]] + ] + | 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 ea192611e66361ddac18c20127d79336ea8dd503 Mon Sep 17 00:00:00 2001 From: Hrithik Saini <49943287+hrithiksaini99@users.noreply.github.com> Date: Fri, 6 Nov 2026 12:08:06 +0530 Subject: [PATCH 3/3] Fold scalar attribute values into the existing overloads Address review: remove the added regression tests per tests/REGRESSION.md, replace the object overload with the existing Scalar union, and fold it into the mapping overloads for set_node_attributes and set_edge_attributes. --- .../check_tricky_function_params-py312.py | 6 ------ stubs/networkx/networkx/_typing.pyi | 3 ++- stubs/networkx/networkx/classes/function.pyi | 15 ++++----------- 3 files changed, 6 insertions(+), 18 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 d7aa3e7bf88c..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,9 +23,3 @@ 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 value + name -nx.set_edge_attributes(G, 5, "weight") -nx.set_edge_attributes(G, "blue", "color") -nx.set_node_attributes(G, 5, "weight") -nx.set_node_attributes(G, "red", "color") diff --git a/stubs/networkx/networkx/_typing.pyi b/stubs/networkx/networkx/_typing.pyi index b82d9673deb0..2c838378fbb8 100644 --- a/stubs/networkx/networkx/_typing.pyi +++ b/stubs/networkx/networkx/_typing.pyi @@ -6,7 +6,8 @@ from typing_extensions import TypeVar import numpy as np -_ScalarT = TypeVar("_ScalarT", bound=bool | int | float | complex | str | bytes | np.generic) +Scalar: TypeAlias = bool | int | float | complex | str | bytes | np.generic +_ScalarT = TypeVar("_ScalarT", bound=Scalar) _GenericT = TypeVar("_GenericT", bound=np.generic) _GenericT_co = TypeVar("_GenericT_co", bound=np.generic, covariant=True) _ShapeT_co = TypeVar("_ShapeT_co", bound=tuple[int, ...], default=Any, covariant=True) diff --git a/stubs/networkx/networkx/classes/function.pyi b/stubs/networkx/networkx/classes/function.pyi index 4a7956a9fb04..d2f33a7505e8 100644 --- a/stubs/networkx/networkx/classes/function.pyi +++ b/stubs/networkx/networkx/classes/function.pyi @@ -3,6 +3,7 @@ from collections.abc import Callable, Collection, Generator, Iterable, Iterator from typing import Any, Literal, TypeVar, overload from networkx import _dispatchable +from networkx._typing import Scalar from networkx.algorithms.planarity import PlanarEmbedding from networkx.classes.digraph import DiGraph from networkx.classes.graph import Graph, _EdgeData, _NBunch, _Node, _NodeData @@ -86,17 +87,13 @@ def create_empty_copy(G: Graph[_Node], with_data: bool = True): ... @overload def set_node_attributes( G: Graph[_Node], - values: SupportsItems[_Node, Unused], + values: SupportsItems[_Node, Unused] | Scalar, name: str, *, 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( - G: Graph[_Node], values: object, name: str, *, backend: str | None = None, **backend_kwargs: Any -) -> None: ... -@overload def set_node_attributes( G: Graph[_Node], values: SupportsItems[_Node, SupportsKeysAndGetItem[Incomplete, Incomplete] | Iterable[tuple[Incomplete, Incomplete]]], @@ -114,7 +111,7 @@ def remove_node_attributes(G: Graph[_Node], *attr_names, nbunch=None) -> None: . @overload def set_edge_attributes( G: MultiGraph[_Node], - values: dict[tuple[_Node, _Node, Incomplete], Incomplete], + values: dict[tuple[_Node, _Node, Incomplete], Incomplete] | Scalar, name: str, *, backend: str | None = None, @@ -123,17 +120,13 @@ def set_edge_attributes( @overload def set_edge_attributes( G: Graph[_Node], - values: SupportsItems[tuple[_Node, _Node], Incomplete], + values: SupportsItems[tuple[_Node, _Node], Incomplete] | Scalar, name: str, *, backend: str | None = None, # @_dispatchable adds these arguments, but we can't use this decorator with @overload **backend_kwargs: Any, ) -> None: ... @overload -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: (