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

CycleError: Report all nodes #159

Open
wants to merge 8 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
24 changes: 19 additions & 5 deletions pytools/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,24 @@ class CycleError(Exception):
"""
Raised when a topological ordering cannot be computed due to a cycle.

:attr node: Node in a directed graph that is part of a cycle.
:attr paths: A :class:`list` in which each element represents another
:class:`list` of nodes that form a cycle. In each cycle,
``node[i+1]`` is a successor of ``node[i]``.
"""
def __init__(self, node: NodeT) -> None:
self.node = node
def __init__(self, paths: List[List[NodeT]]) -> None:
self.paths = paths

def __str__(self) -> str:
le = len(self.paths)
mlen = 10
return f"{[n for n in self.paths[:mlen]] + (['...'] if le > mlen else [])}"

@property
def node(self) -> Hashable:
from warnings import warn
warn("CycleError.node is deprecated and will go away in 06/2023. "
"Use CycleError.nodes instead.", DeprecationWarning)
return self.paths[0][0]


class HeapEntry:
Expand Down Expand Up @@ -317,8 +331,8 @@ def compute_topological_order(graph: GraphT[NodeT],

if len(order) != total_num_nodes:
# any node which has a predecessor left is a part of a cycle
raise CycleError(next(iter(n for n, num_preds in
nodes_to_num_predecessors.items() if num_preds != 0)))
raise CycleError([list(n for n, num_preds in
nodes_to_num_predecessors.items() if num_preds != 0)])

return order

Expand Down
4 changes: 4 additions & 0 deletions test/test_graph_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ def test_compute_topological_order():
with pytest.raises(CycleError):
compute_topological_order(cycle)

cycle2 = {0: [2], 1: [2], 2: [3], 3: [1]}
with pytest.raises(CycleError, match=r"\[1, 2, 3\]"):
compute_topological_order(cycle2)


def test_transitive_closure():
from pytools.graph import compute_transitive_closure
Expand Down