Skip to content

Commit ea19261

Browse files
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.
1 parent 57e7cd9 commit ea19261

3 files changed

Lines changed: 6 additions & 18 deletions

File tree

‎stubs/networkx/@tests/test_cases/check_tricky_function_params-py312.py‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,3 @@
2323
assert_type(degree_view(""), int)
2424
assert_type(degree_view([""]), DegreeView[str])
2525
assert_type(degree_view({""}), DegreeView[str])
26-
27-
# Test set_edge_attributes and set_node_attributes with scalar value + name
28-
nx.set_edge_attributes(G, 5, "weight")
29-
nx.set_edge_attributes(G, "blue", "color")
30-
nx.set_node_attributes(G, 5, "weight")
31-
nx.set_node_attributes(G, "red", "color")

‎stubs/networkx/networkx/_typing.pyi‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ from typing_extensions import TypeVar
66

77
import numpy as np
88

9-
_ScalarT = TypeVar("_ScalarT", bound=bool | int | float | complex | str | bytes | np.generic)
9+
Scalar: TypeAlias = bool | int | float | complex | str | bytes | np.generic
10+
_ScalarT = TypeVar("_ScalarT", bound=Scalar)
1011
_GenericT = TypeVar("_GenericT", bound=np.generic)
1112
_GenericT_co = TypeVar("_GenericT_co", bound=np.generic, covariant=True)
1213
_ShapeT_co = TypeVar("_ShapeT_co", bound=tuple[int, ...], default=Any, covariant=True)

‎stubs/networkx/networkx/classes/function.pyi‎

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ from collections.abc import Callable, Collection, Generator, Iterable, Iterator
33
from typing import Any, Literal, TypeVar, overload
44

55
from networkx import _dispatchable
6+
from networkx._typing import Scalar
67
from networkx.algorithms.planarity import PlanarEmbedding
78
from networkx.classes.digraph import DiGraph
89
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): ...
8687
@overload
8788
def set_node_attributes(
8889
G: Graph[_Node],
89-
values: SupportsItems[_Node, Unused],
90+
values: SupportsItems[_Node, Unused] | Scalar,
9091
name: str,
9192
*,
9293
backend: str | None = None, # @_dispatchable adds these arguments, but we can't use this decorator with @overload
9394
**backend_kwargs: Any,
9495
) -> None: ...
9596
@overload
96-
def set_node_attributes(
97-
G: Graph[_Node], values: object, name: str, *, backend: str | None = None, **backend_kwargs: Any
98-
) -> None: ...
99-
@overload
10097
def set_node_attributes(
10198
G: Graph[_Node],
10299
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: .
114111
@overload
115112
def set_edge_attributes(
116113
G: MultiGraph[_Node],
117-
values: dict[tuple[_Node, _Node, Incomplete], Incomplete],
114+
values: dict[tuple[_Node, _Node, Incomplete], Incomplete] | Scalar,
118115
name: str,
119116
*,
120117
backend: str | None = None,
@@ -123,17 +120,13 @@ def set_edge_attributes(
123120
@overload
124121
def set_edge_attributes(
125122
G: Graph[_Node],
126-
values: SupportsItems[tuple[_Node, _Node], Incomplete],
123+
values: SupportsItems[tuple[_Node, _Node], Incomplete] | Scalar,
127124
name: str,
128125
*,
129126
backend: str | None = None, # @_dispatchable adds these arguments, but we can't use this decorator with @overload
130127
**backend_kwargs: Any,
131128
) -> None: ...
132129
@overload
133-
def set_edge_attributes(
134-
G: Graph[_Node], values: object, name: str, *, backend: str | None = None, **backend_kwargs: Any
135-
) -> None: ...
136-
@overload
137130
def set_edge_attributes(
138131
G: Graph[_Node],
139132
values: (

0 commit comments

Comments
 (0)