Skip to content

[networkx] Allow scalar values in set_edge_attributes and set_node_attributes - #16698

Open
hrithiksaini99 wants to merge 1 commit into
python:mainfrom
hrithiksaini99:fix-networkx-set-attributes-scalar
Open

hrithiksaini99 wants to merge 1 commit into
python:mainfrom
hrithiksaini99:fix-networkx-set-attributes-scalar

Conversation

@hrithiksaini99

Copy link
Copy Markdown
Contributor

Fixes #16542.

In networkx, set_edge_attributes(G, values, name=None) and set_node_attributes(G, values, name=None) support setting an attribute to a scalar value (e.g. nx.set_edge_attributes(G, 5, "weight"), nx.set_node_attributes(G, "red", "color")). When name is provided and values is not a dictionary, NetworkX treats values as a constant attribute value applied to all edges or nodes.

Previously, set_edge_attributes and set_node_attributes only accepted SupportsItems when name was supplied, causing mypy to reject valid scalar values with:

Argument 2 to "set_edge_attributes" has incompatible type "int"; expected "SupportsItems[tuple[Any, Any], Any]"

This PR:

  • Allows values: object when name: str in set_edge_attributes and set_node_attributes (matching both scalar constants and dicts).
  • Preserves dictionary item requirements for both MultiGraph and Graph when name: None = None.
  • Adds test cases in check_tricky_function_params-py312.py verifying scalar and dictionary attribute calls on graphs and multigraphs.

@github-actions

github-actions Bot commented Nov 4, 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

1 participant