Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not treat missing ancestor as input mismatch #14

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/cyclebane/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,8 @@ def __setitem__(self, branch: Hashable | slice, other: Graph) -> None:
intersection_nodes = set(graph.nodes) & set(new_branch.nodes) - {branch}

for node in intersection_nodes:
if graph.pred[node] != new_branch.pred[node]:
new_pred = new_branch.pred[node]
if new_pred and graph.pred[node] != new_pred:
Comment on lines +433 to +434
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More generally, would it make sense to check if all keys in "new" have the same values in existing, basically if a dict update would have no effect?

raise ValueError(
f"Node inputs differ for node '{node}':\n"
f" {graph.pred[node]}\n"
Expand Down
43 changes: 43 additions & 0 deletions tests/graph_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,49 @@ def test_setitem_preserves_nodes_that_are_ancestors_of_unrelated_node() -> None:
nx.utils.graphs_equal(graph.to_networkx(), g)


def test_setitem_inserts_graph_with_missing_parent() -> None:
g1 = nx.DiGraph()
g1.add_edge('a', 'b')
g1.add_edge('n', 'c')
g2 = nx.DiGraph()
g2.add_edge('b', 'n')

graph = cb.Graph(g1)
graph['n'] = cb.Graph(g2)
expected = g1.copy()
expected.add_edge('b', 'n')
nx.utils.graphs_equal(graph.to_networkx(), expected)


def test_setitem_fails_with_partial_parent_mismatch() -> None:
g1 = nx.DiGraph()
g1.add_edge('a', 'b') # only in g1
g1.add_edge('e', 'b') # in both graphs
g1.add_edge('n', 'c')
g2 = nx.DiGraph()
g2.add_edge('b', 'n')
g2.add_edge('e', 'b') # in both graphs
g2.add_edge('x', 'b') # only in g2

graph = cb.Graph(g1)
with pytest.raises(ValueError, match="Node inputs differ for node 'b'"):
graph['n'] = cb.Graph(g2)


def test_setitem_fails_when_grandparents_change() -> None:
g1 = nx.DiGraph()
g1.add_edge('a1', 'b')
g1.add_edge('a2', 'b')
g1.add_edge('b', 'c')
g2 = nx.DiGraph() # no a2 -> b edge
g2.add_edge('a1', 'b')
g2.add_edge('b', 'c')

graph = cb.Graph(g1)
with pytest.raises(ValueError, match="Node inputs differ for node 'b'"):
graph['c'] = cb.Graph(g2)


def test_getitem_returns_graph_containing_only_key_and_ancestors() -> None:
g = nx.DiGraph()
g.add_edge('a', 'b')
Expand Down
Loading