Skip to content

fix: add_edges_from drops all edges when given a generator#185

Open
arpitjain099 wants to merge 1 commit into
trailofbits:masterfrom
arpitjain099:chore/add-edges-generator
Open

fix: add_edges_from drops all edges when given a generator#185
arpitjain099 wants to merge 1 commit into
trailofbits:masterfrom
arpitjain099:chore/add-edges-generator

Conversation

@arpitjain099

@arpitjain099 arpitjain099 commented Jun 28, 2026

Copy link
Copy Markdown

RootedDiGraph.add_edges_from consumes ebunch_to_add in its own loop to register root nodes, then passes that same argument on to super().add_edges_from(...). If a caller hands it a generator, the loop exhausts it and the super call sees nothing, so the graph ends up with zero edges. NetworkX documents the method as accepting any iterable of edge-tuples, generators included. The sibling add_nodes_from already passes its materialized list down, so this just brings the two into line by handing super the edges list we already built. It's masked today because the only internal call site passes a re-iterable view.

Added regression tests in test/test_graphs.py for generator input (2-tuples and 3-tuples with an attr dict, plus the node case); they fail on the current code and pass with the fix.

RootedDiGraph.add_edges_from iterates ebunch_to_add to register root
nodes and build a reconstructed edges list, but then passes the original
ebunch_to_add to super().add_edges_from instead of the materialized list.

When ebunch_to_add is a single-pass iterable (a generator), the first
loop fully consumes it, so the super() call receives an exhausted
iterator and adds zero edges. The NetworkX add_edges_from contract
accepts an iterable of edge-tuples, which includes generators, so this
is a silent correctness bug. The sibling add_nodes_from already passes
its materialized list correctly.

Pass the already-built edges list to super(), matching add_nodes_from.
Add regression tests covering generator input for add_edges_from
(2-tuples and 3-tuples with an attr dict) and add_nodes_from.

Signed-off-by: Arpit Jain <arpitjain099@gmail.com>
@CLAassistant

CLAassistant commented Jun 28, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

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.

2 participants