diff --git a/wizard-work/beam/toposort/toposort.py b/wizard-work/beam/toposort/toposort.py index 0faaf31..22ab30a 100644 --- a/wizard-work/beam/toposort/toposort.py +++ b/wizard-work/beam/toposort/toposort.py @@ -170,7 +170,12 @@ def topo_sort(graph: Mapping[Any, Iterable[Any]]) -> TopoResult: def _key(x): + tn = type(x).__name__ + # Probe whether instances of this type support ordering; if not, fall back + # to a repr/id-based key so mixed or unorderable node types never crash + # the ready-queue sort or cycle reconstruction. try: - return (0, type(x).__name__, x) - except TypeError: - return (1, id(x)) \ No newline at end of file + _ = x < x + except (TypeError, Exception): + return (1, tn, repr(x), id(x)) + return (0, tn, x) \ No newline at end of file