Title: networkx: generic typing for node and edge data · Issue #15659 · python/typeshed · GitHub
Open Graph Title: networkx: generic typing for node and edge data · Issue #15659 · python/typeshed
X Title: networkx: generic typing for node and edge data · Issue #15659 · python/typeshed
Description: Currently there are no proper type hints for the data carried by nodes and edges. Using generics, I propose we get the following functionality: class NodeData(TypedDict): x: int y: int class EdgeData(TypedDict): id: int color: str G = nx...
Open Graph Description: Currently there are no proper type hints for the data carried by nodes and edges. Using generics, I propose we get the following functionality: class NodeData(TypedDict): x: int y: int class EdgeDa...
X Description: Currently there are no proper type hints for the data carried by nodes and edges. Using generics, I propose we get the following functionality: class NodeData(TypedDict): x: int y: int class EdgeDa...
Opengraph URL: https://github.com/python/typeshed/issues/15659
X: @github
Domain: github.com
IPv4 address: 140.82.114.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"networkx: generic typing for node and edge data","articleBody":"Currently there are no proper type hints for the data carried by nodes and edges. \n\nUsing generics, I propose we get the following functionality:\n```python\n\nclass NodeData(TypedDict):\n x: int\n y: int\n\nclass EdgeData(TypedDict):\n id: int\n color: str\n\nG = nx.Graph[int, NodeData, EdgeData]()\n\nG.add_nodes_from([(1, NodeData(x=0, y=0)), (2, NodeData(x=25, y=30))])\nnodes = G.nodes(data=True)\ndata = nodes[3] # data: NodeData\n\nG.add_edges_from([(1, 2, EdgeData(id=5, color=\"red\"))])\n\nedges = G.edges(data=True)\ndata = G.get_edge_data(1, 2) # data: EdgeData\n\n\nG.add_edges_from([(1, 2, EdgeData(id=5, color=\"red\"))]) # correct\nG.add_edges_from([(1, 2, EdgeData(id=\"5\", color=\"red\"))]) # error on id\nG.add_edges_from([(1, 2, EdgeData(x__coord=10, y__coord=20))]) # error on coords\n```\n\nThe default could be set to `dict[str,Any]`, this should also keep compatibility with existing projects as it allows for `nx.Graph[Node]()` and matches most existing stubs.\n\nUnfortunately, it does not seem possible to enforce these types on insertion. In the `add_node()` and `add_edge()` functions due to `**attr` in their signatures, `Unpack` does not work for generics. In `add_nodes_from()` and `add_edges_from()` we must allow _Node and _Edge respectively in the ebunch (although we could enforce `_NodeWithData` and `_EdgeWithData` to facilitate a fully typed workflow, but that would differ from the implementation).","author":{"url":"https://github.com/ThePiep","@type":"Person","name":"ThePiep"},"datePublished":"2026-04-21T18:45:58Z","url":"https://github.com/python/typeshed/issues/15659"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:42663452-c3d7-730c-af8e-6f5cb26f5c68 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9F0E:110ABE:122F6D:18C678:6B06B9B1 |
| html-safe-nonce | 43036cb5a1f54b3d2998fbb05a97006f91308f50b863c094575b8409ed87782d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5RjBFOjExMEFCRToxMjJGNkQ6MThDNjc4OjZCMDZCOUIxIiwidmlzaXRvcl9pZCI6IjU0NTU4MTU2NTg0Njg0NTY4ODEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 4ba48766049ec88d5918f560ece0d4b139c0a2a31ed7ed6aadba0a4843aaff25 |
| hovercard-subject-tag | issue:4304650409 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/typeshed/15659/issue_layout |
| twitter:image | https://opengraph.githubassets.com/fbf6600df89c393ccd81720229cc9dbedc56dc5bf797e010d7eb93b5d5ba287c/python/typeshed/issues/15659 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/fbf6600df89c393ccd81720229cc9dbedc56dc5bf797e010d7eb93b5d5ba287c/python/typeshed/issues/15659 |
| og:image:alt | Currently there are no proper type hints for the data carried by nodes and edges. Using generics, I propose we get the following functionality: class NodeData(TypedDict): x: int y: int class EdgeDa... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ThePiep |
| hostname | github.com |
| expected-hostname | github.com |
| None | 32031aaff792fa7ad778f7418e1fbfc0d525a5779804329b769aa75230448a18 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/typeshed git https://github.com/python/typeshed.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 31696383 |
| octolytics-dimension-repository_nwo | python/typeshed |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 31696383 |
| octolytics-dimension-repository_network_root_nwo | python/typeshed |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 37014d914b7d14eede971655595f687a0202af40 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width