Skip to content

[networkx] Accept any node/edge data types in graph parameters outside algorithms - #16387

Open
ekanshul wants to merge 3 commits into
python:mainfrom
ekanshul:networkx-graph-data-params
Open

[networkx] Accept any node/edge data types in graph parameters outside algorithms#16387
ekanshul wants to merge 3 commits into
python:mainfrom
ekanshul:networkx-graph-data-params

Conversation

@ekanshul

Copy link
Copy Markdown
Contributor

Part of #16365, following up on #16371.

Outside networkx.algorithms, 33 modules still annotated graph parameters as Graph[_Node], which with the defaults of _NodeData and _EdgeData means Graph[_Node, dict[str, Any], dict[str, Any]]. A graph whose node or edge attribute dicts are some other Mapping (the Graph[int, Bar] from the report) is therefore rejected by nx.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 the DiGraph/MultiGraph equivalents), matching networkx.classes and the drawing package: classes.function, convert, convert_matrix, generators, linalg, readwrite (including json_graph) and utils. Only parameter annotations change; functions that build new graphs keep returning Graph[_Node], i.e. with the default dict data.

Two details:

  • edge_subgraph() and restricted_view() return views of the input graph, so their return type follows the input instead of Graph[Incomplete].
  • The two selfloop_edges() overloads that are also generic in _U (the default= value) use Graph[_Node, Any, Any], because pyright rejects TypeVars with defaults appearing before one without, as in nx_latex in [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 in networkx.algorithms can 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

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

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

Copy link
Copy Markdown
Contributor

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

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.

1 participant