Skip to content

Commit 2a55d73

Browse files
authored
Introduce PathsNotGreaterThanOneError (FV2310 E_0266) (#102)
1 parent f0905a7 commit 2a55d73

4 files changed

Lines changed: 461 additions & 5 deletions

File tree

src/ebdtable2graph/graph_utils.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from networkx import DiGraph, all_simple_paths # type:ignore[import]
99

1010
from ebdtable2graph.models import ToNoEdge, ToYesEdge
11+
from ebdtable2graph.models.errors import PathsNotGreaterThanOneError
1112

1213
COMMON_ANCESTOR_FIELD = "common_ancestor_for_node"
1314
# Defines the label to annotate the last common ancestor node with the information to which node
@@ -43,7 +44,12 @@ def _mark_last_common_ancestors(graph: DiGraph) -> None:
4344
if in_degree <= 1:
4445
continue
4546
paths = list(all_simple_paths(graph, source="Start", target=node))
46-
assert len(paths) > 1, "If indegree > 1, the number of paths should always be greater than 1 too."
47+
if len(paths) <= 1:
48+
raise PathsNotGreaterThanOneError(
49+
node_key=node,
50+
indegree=in_degree,
51+
number_of_paths=len(paths),
52+
)
4753
common_ancestor = _find_last_common_ancestor(paths)
4854
assert common_ancestor != "Start", "Last common ancestor should always be at least the first decision node '1'."
4955
# Annotate the common ancestor for later conversion

src/ebdtable2graph/models/errors/__init__.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,18 @@ def __init__(self, msg: str, decision_node_key, outgoing_edges: list[str]):
2121

2222
def __str__(self):
2323
return f"The node {self.decision_node_key} has more than 2 outgoing edges: {', '.join(self.outgoing_edges)}"
24+
25+
26+
class PathsNotGreaterThanOneError(ValueError):
27+
"""
28+
If indegree > 1, the number of paths should always be greater than 1 too.
29+
Typically, this is a symptom for loops in the graph (which makes them not a Directed Graph / tree anymore).
30+
"""
31+
32+
def __init__(self, node_key: str, indegree: int, number_of_paths: int):
33+
super().__init__(
34+
f"The indegree of node '{node_key}' is {indegree} > 1, but the number of paths is {number_of_paths} <= 1."
35+
)
36+
self.node_key = node_key
37+
self.indegree = indegree
38+
self.number_of_paths = number_of_paths

0 commit comments

Comments
 (0)