Skip to content

[networkx] Allow scalar values with attribute name in set_edge_attributes and set_node_attributes - #16693

Open
hrithiksaini99 wants to merge 3 commits into
python:mainfrom
hrithiksaini99:fix-networkx-set-attributes
Open

hrithiksaini99 wants to merge 3 commits into
python:mainfrom
hrithiksaini99:fix-networkx-set-attributes

Conversation

@hrithiksaini99

Copy link
Copy Markdown
Contributor

Fixes #16542

NetworkX's set_edge_attributes and set_node_attributes accept 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 when name is provided:

>>> import networkx as nx
>>> G = nx.path_graph(3)
>>> nx.set_edge_attributes(G, 5, "some name")
>>> G.edges[0, 1]["some name"]
5

Previously, the stubs only accepted SupportsItems or dict when name: str, causing type checkers to reject calls with scalar values and an attribute name.

This PR:

  • Adds overloads for set_edge_attributes and set_node_attributes with values: object when name: str.
  • Adds test cases in stubs/networkx/@tests/test_cases/check_tricky_function_params-py312.py.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

Comment on lines +27 to +31
# 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")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See previous comments about tests.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +95 to +97
@overload
def set_node_attributes(
G: Graph[_Node], values: object, name: str, *, backend: str | None = None, **backend_kwargs: Any

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

networkx.set_edge_attributes should support scalar value + name

2 participants