Repository navigation
[networkx] Allow scalar values with attribute name in set_edge_attributes and set_node_attributes - #16693
[networkx] Allow scalar values with attribute name in set_edge_attributes and set_node_attributes#16693hrithiksaini99 wants to merge 3 commits into
Conversation
…utes and set_node_attributes
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
| # 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") |
There was a problem hiding this comment.
See previous comments about tests.
There was a problem hiding this comment.
Removed — the test cases added by this PR are gone, and the Scalar alias is folded into the existing mapping overloads instead of adding a separate object overload. Thanks for the pointer to tests/REGRESSION.md.
| @overload | ||
| def set_node_attributes( | ||
| G: Graph[_Node], values: object, name: str, *, backend: str | None = None, **backend_kwargs: Any |
There was a problem hiding this comment.
I'm not certain what this overload does, especially since it completely overrides the previous one. object also doesn't mean "any scalar value", we need a better annotation and can then fold this overload into the previous one.
There was a problem hiding this comment.
Reworked. There is no longer a separate object overload: the existing values: SupportsItems[_Node, Unused] overload now reads values: SupportsItems[_Node, Unused] | Scalar, where Scalar is the bool | int | float | complex | str | bytes | np.generic alias already defined in networkx._typing (it was previously only inlined into _ScalarT's bound). That names the scalar set explicitly instead of using object, and the overload no longer shadows the previous one.
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.
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
Fixes #16542
NetworkX's
set_edge_attributesandset_node_attributesaccept either a dict-like mapping (mapping edges/nodes to values) or any scalar value (e.g. int, float, str, etc.) applied to all edges/nodes whennameis provided:Previously, the stubs only accepted
SupportsItemsordictwhenname: str, causing type checkers to reject calls with scalarvaluesand an attributename.This PR:
set_edge_attributesandset_node_attributeswithvalues: objectwhenname: str.stubs/networkx/@tests/test_cases/check_tricky_function_params-py312.py.