[networkx] Accept any node/edge data types in graph parameters outside algorithms - #16387
Open
ekanshul wants to merge 3 commits into
Open
[networkx] Accept any node/edge data types in graph parameters outside algorithms#16387ekanshul wants to merge 3 commits into
algorithms#16387ekanshul wants to merge 3 commits into
Conversation
…e `algorithms` Graph parameters annotated as `Graph[_Node]` only accept the default `dict[str, Any]` node and edge data, so graphs with other `Mapping` data types are rejected by functions that never look at the data. Type them as `Graph[_Node, _NodeData, _EdgeData]` in `classes.function`, `convert`, `convert_matrix`, `generators`, `linalg`, `readwrite` and `utils`, as already done for `classes` and `drawing`. Return types of graph-building functions are unchanged; `edge_subgraph()` and `restricted_view()` now return the input graph's type. Part of python#16365. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This comment has been minimized.
This comment has been minimized.
The other networkx test cases carry the -py312 suffix because numpy's stubs no longer type-check under --python-version 3.10/3.11, and the strict test-case pyright config reports partially unknown types for functions whose return is unannotated or typed with scipy. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Contributor
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
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.
Part of #16365, following up on #16371.
Outside
networkx.algorithms, 33 modules still annotated graph parameters asGraph[_Node], which with the defaults of_NodeDataand_EdgeDatameansGraph[_Node, dict[str, Any], dict[str, Any]]. A graph whose node or edge attribute dicts are some otherMapping(theGraph[int, Bar]from the report) is therefore rejected bynx.number_of_nodes(),nx.write_gml(),nx.adjacency_matrix(),nx.to_dict_of_lists()and friends, even though none of them care about the data type.This types those parameters as
Graph[_Node, _NodeData, _EdgeData](and theDiGraph/MultiGraphequivalents), matchingnetworkx.classesand thedrawingpackage:classes.function,convert,convert_matrix,generators,linalg,readwrite(includingjson_graph) andutils. Only parameter annotations change; functions that build new graphs keep returningGraph[_Node], i.e. with the defaultdictdata.Two details:
edge_subgraph()andrestricted_view()return views of the input graph, so their return type follows the input instead ofGraph[Incomplete].selfloop_edges()overloads that are also generic in_U(thedefault=value) useGraph[_Node, Any, Any], because pyright rejects TypeVars with defaults appearing before one without, as innx_latexin [networkx] Accept any node and edge data types in the drawing functions #16371.A test case covers a graph with a
Mapping-based node data type against functions from each touched package, plus the view return types and the unchanged default case.The ~600 remaining
Graph[_Node]parameters innetworkx.algorithmscan be done the same way in one or two follow-ups.mypy, pyright, stubtest and the regression test cases pass locally.
Agent used: Claude Code
🤖 Generated with Claude Code