Skip to content

Improve error message when the graph contains cyclic dependencies #33

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
19 changes: 17 additions & 2 deletions graphkit/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ class DeleteInstruction(str):
def __repr__(self):
return 'DeleteInstruction("%s")' % self

def _format_cycle(cycle):
operation_strings = []
for second, first in reversed(cycle[1:] + [cycle[0]]):
if isinstance(first, DataPlaceholderNode) and operation_strings:
operation_strings[-1] += f' needs "{first}" from {second.name}.'
if isinstance(first, Operation):
operation_strings.append(first.name)
return "\n".join(operation_strings)

class Network(object):
"""
Expand Down Expand Up @@ -107,8 +115,15 @@ def compile(self):
self.steps = []

# create an execution order such that each layer's needs are provided.
ordered_nodes = list(nx.dag.topological_sort(self.graph))

try:
ordered_nodes = list(nx.dag.topological_sort(self.graph))
except nx.exception.NetworkXUnfeasible as ex:
try:
cycle = nx.find_cycle(self.graph)
except nx.exception.NetworkXNoCycle:
raise ex
message = f"Cyclic dependency in graph:\n{_format_cycle(cycle)}"
raise nx.exception.NetworkXUnfeasible(message)
# add Operations evaluation steps, and instructions to free data.
for i, node in enumerate(ordered_nodes):

Expand Down