[networkx] Fix NodeDataView return type for string attribute in nodes(data=...) - #16695
Open
hrithiksaini99 wants to merge 1 commit into
Open
hrithiksaini99 wants to merge 1 commit into
hrithiksaini99 wants to merge 1 commit into
Conversation
…(data=...) (python#16548) - Update NodeView.__call__ and NodeView.data overloads to return NodeDataView[_Node, _U] when data is a string - Make NodeDataView generic over [_Node, _D] so __iter__ yields tuple[_Node, _D] and __getitem__ returns _D - Fixes python#16548
Contributor
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
srittau
requested changes
Nov 4, 2026
|
|
||
| @overload | ||
| def __call__(self, data: Literal[False] = False, default=None) -> Self: ... | ||
| def __call__(self, data: Literal[False] = False, default: Unused = None) -> Self: ... |
Collaborator
There was a problem hiding this comment.
I think we should try to remove the default argument for this branch to catch misuse of this API. If that doesn't work, try to use None as type argument.
| def __call__(self, data: Literal[False] = False, default: Unused = None) -> Self: ... | ||
| @overload | ||
| def __call__(self, data: Literal[True] | str, default=None) -> NodeDataView[_Node, _NodeData, _EdgeData]: ... | ||
| def __call__(self, data: Literal[True], default: None = None) -> NodeDataView[_Node, _NodeData]: ... |
Comment on lines
+53
to
+55
| def data(self, data: Literal[False], default: Unused = None) -> Self: ... | ||
| @overload | ||
| def data(self, data: Literal[True] = True, default: None = None) -> NodeDataView[_Node, _NodeData]: ... |
| def data(self, data: str, default: _U | None = None) -> NodeDataView[_Node, _U]: ... | ||
|
|
||
| class NodeDataView(AbstractSet[_Node], Generic[_Node, _NodeData, _EdgeData]): | ||
| class NodeDataView(AbstractSet[_Node], Generic[_Node, _D]): |
| class NodeDataView(AbstractSet[_Node], Generic[_Node, _D]): | ||
| __slots__ = ("_nodes", "_data", "_default") | ||
| def __init__(self, nodedict: _NodeData, data: bool | str = False, default=None) -> None: ... | ||
| def __init__(self, nodedict, data: bool | str = False, default: object = None) -> None: ... |
Collaborator
There was a problem hiding this comment.
Why remove the annotation for nodedict?
Why use object for default? How is it used?
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #16548
According to the NetworkX documentation and runtime behavior for
Graph.nodes(data=...):Previously,
NodeView.__call__andNodeView.datatypeddata: Literal[True] | stras returningNodeDataView[_Node, _NodeData, _EdgeData], which always yieldedtuple[_Node, _NodeData](the full dictionary) even when an attribute string was requested.This PR:
data: Literal[True](returningNodeDataView[_Node, _NodeData]) anddata: str(returningNodeDataView[_Node, _U], generic over the extracted attribute type).NodeDataViewto beGeneric[_Node, _D], yieldingtuple[_Node, _D]on iteration and returning_Don item lookup (matchingOutEdgeDataView).