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..330b799b1d6b 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 @@ -82,15 +82,14 @@ 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], 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( @@ -98,8 +97,12 @@ 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: str | None = None, **backend_kwargs: Any ) -> None: ... @_dispatchable @@ -114,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( @@ -123,10 +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, name: None = None, *, 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: Any, 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]: ...