Skip to content

Commit f71dc2b

Browse files
CycleError: Report all nodes
1 parent b04c440 commit f71dc2b

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

pytools/graph.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,10 @@ class CycleError(Exception):
212212
"""
213213
Raised when a topological ordering cannot be computed due to a cycle.
214214
215-
:attr node: Node in a directed graph that is part of a cycle.
215+
:attr nodes: List of nodes in a directed graph that are part of a cycle.
216216
"""
217-
def __init__(self, node: T) -> None:
218-
self.node = node
217+
def __init__(self, nodes: List[T]) -> None:
218+
self.nodes = nodes
219219

220220

221221
class HeapEntry:
@@ -299,8 +299,8 @@ def compute_topological_order(graph: Mapping[T, Collection[T]],
299299

300300
if len(order) != total_num_nodes:
301301
# any node which has a predecessor left is a part of a cycle
302-
raise CycleError(next(iter(n for n, num_preds in
303-
nodes_to_num_predecessors.items() if num_preds != 0)))
302+
raise CycleError(list(n for n, num_preds in
303+
nodes_to_num_predecessors.items() if num_preds != 0))
304304

305305
return order
306306

test/test_graph_tools.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ def test_compute_topological_order():
7676
with pytest.raises(CycleError):
7777
compute_topological_order(cycle)
7878

79+
cycle2 = {0: [2], 1: [2], 2: [3], 3: [1]}
80+
with pytest.raises(CycleError) as exc:
81+
compute_topological_order(cycle2)
82+
83+
assert str(exc.value) == "[1, 2, 3]"
84+
7985

8086
def test_transitive_closure():
8187
from pytools.graph import compute_transitive_closure

0 commit comments

Comments
 (0)