Skip to content

Commit d9a6f0e

Browse files
[networkx] Allow scalar values with attribute name in set_edge_attributes and set_node_attributes (#16693)
1 parent 076613f commit d9a6f0e

2 files changed

Lines changed: 30 additions & 16 deletions

File tree

‎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: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from _typeshed import Incomplete, SupportsItems, SupportsKeysAndGetItem, Unused
22
from collections.abc import Callable, Collection, Generator, Iterable, Iterator
3-
from typing import Literal, TypeVar, overload
3+
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
@@ -82,51 +83,63 @@ def to_directed(graph): ...
8283
def to_undirected(graph): ...
8384
def create_empty_copy(G: Graph[_Node], with_data: bool = True): ...
8485

85-
# incomplete: Can "Any scalar value" be enforced?
86+
# Values can be a dict-like mapping or any scalar value when name is provided
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
*,
92-
backend=None, # @_dispatchable adds these arguments, but we can't use this decorator with @overload
93-
**backend_kwargs,
93+
backend: str | None = None, # @_dispatchable adds these arguments, but we can't use this decorator with @overload
94+
**backend_kwargs: Any,
9495
) -> None: ...
9596
@overload
9697
def set_node_attributes(
9798
G: Graph[_Node],
9899
values: SupportsItems[_Node, SupportsKeysAndGetItem[Incomplete, Incomplete] | Iterable[tuple[Incomplete, Incomplete]]],
99100
name: None = None,
100101
*,
101-
backend=None,
102-
**backend_kwargs,
102+
backend: str | None = None,
103+
**backend_kwargs: Any,
103104
) -> None: ...
104105

105106
@_dispatchable
106107
def get_node_attributes(G: Graph[_Node], name: str, default=None) -> dict[_Node, Incomplete]: ...
107108
@_dispatchable
108109
def remove_node_attributes(G: Graph[_Node], *attr_names, nbunch=None) -> None: ...
109110

111+
@overload
112+
def set_edge_attributes(
113+
G: MultiGraph[_Node],
114+
values: dict[tuple[_Node, _Node, Incomplete], Incomplete] | Scalar,
115+
name: str,
116+
*,
117+
backend: str | None = None,
118+
**backend_kwargs: Any,
119+
) -> None: ...
110120
@overload
111121
def set_edge_attributes(
112122
G: Graph[_Node],
113-
values: SupportsItems[tuple[_Node, _Node], Incomplete],
123+
values: SupportsItems[tuple[_Node, _Node], Incomplete] | Scalar,
114124
name: str,
115125
*,
116126
backend: str | None = None, # @_dispatchable adds these arguments, but we can't use this decorator with @overload
117-
**backend_kwargs,
127+
**backend_kwargs: Any,
118128
) -> None: ...
119129
@overload
120130
def set_edge_attributes(
121-
G: MultiGraph[_Node],
122-
values: dict[tuple[_Node, _Node, Incomplete], Incomplete],
123-
name: str,
131+
G: Graph[_Node],
132+
values: (
133+
SupportsItems[
134+
tuple[_Node, _Node], SupportsKeysAndGetItem[Incomplete, Incomplete] | Iterable[tuple[Incomplete, Incomplete]]
135+
]
136+
| Incomplete
137+
),
138+
name: None = None,
124139
*,
125140
backend: str | None = None,
126-
**backend_kwargs,
141+
**backend_kwargs: Any,
127142
) -> None: ...
128-
@overload
129-
def set_edge_attributes(G: Graph[_Node], values, name: None = None, *, backend: str | None = None, **backend_kwargs) -> None: ...
130143

131144
@_dispatchable
132145
def get_edge_attributes(G: Graph[_Node], name: str, default=None) -> dict[tuple[_Node, _Node], Incomplete]: ...

0 commit comments

Comments
 (0)