Skip to content

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

Open
Fire162 wants to merge 5 commits into
python:mainfrom
Fire162:fix-networkx-set-attributes
Open

Fire162 wants to merge 5 commits into
python:mainfrom
Fire162:fix-networkx-set-attributes

Conversation

@Fire162

@Fire162 Fire162 commented Oct 24, 2026

Copy link
Copy Markdown

Fixes #16542

NetworkX supports passing scalar values to set_edge_attributes and set_node_attributes when name is provided (applying the scalar value to all edges or nodes). Added an overload accepting values: object, name: str to both functions.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

*,
backend=None, # @_dispatchable adds these arguments, but we can't use this decorator with @overload
**backend_kwargs,
backend: str | None = None, # @_dispatchable adds these arguments, but we can't use this decorator with @overload

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.

Let's also remove the comment, which doesn't help us much.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Removed the comment.

backend=None, # @_dispatchable adds these arguments, but we can't use this decorator with @overload
**backend_kwargs,
backend: str | None = None, # @_dispatchable adds these arguments, but we can't use this decorator with @overload
**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.

Why Any here? Please note that all uses of Any (except obvious ones) need to be documented. Could we reasonably replace **backend_kwargs with these kwargs? Then leave it unannotated, until we replace it. Are the backend_kwargs dependent on the particular backend (if there are multiple)? Document this here with a comment.

(More instances below.)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good point, reverted **backend_kwargs to be unannotated across all overloads.

) -> None: ...
@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.

Same here.

Also, why is object used for values?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

NetworkX accepts any scalar or arbitrary Python object (integers, strings, floats, lists, etc.) when name is provided, setting it directly as the attribute value on each node/edge (G.nodes[n][name] = values). object seemed the most fitting type for an arbitrary scalar attribute value. For name=None, I reverted values to be unannotated as well.

Comment on lines +132 to +138
def set_edge_attributes(
G: Graph[_Node], values: object, name: str, *, backend: str | None = None, **backend_kwargs: Any
) -> None: ...
@overload
def set_edge_attributes(
G: Graph[_Node], values: Any, name: None = None, *, backend: str | None = None, **backend_kwargs: Any
) -> None: ...

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.

Same problems as above: Why Any/object for values?

Fire162 and others added 2 commits October 28, 2026 20:59
Address review feedback by leaving **backend_kwargs unannotated, reverting
values to unannotated for set_edge_attributes when name is None, and removing
obsolete @_dispatchable comments.
@github-actions

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