From 382f5e0c212178121e993f5d7de53953e95588d3 Mon Sep 17 00:00:00 2001 From: Petru Conduraru Date: Sun, 12 Jul 2026 08:00:55 +0300 Subject: [PATCH] ifcpatch: use stdlib graphlib for Optimise topological sort (#4399) The Optimise recipe imported `toposort`, a third-party PyPI package that is not bundled with Bonsai, so running the recipe there raised `ModuleNotFoundError: No module named 'toposort'`. Replace it with the standard library `graphlib.TopologicalSorter` (available since Python 3.9), which provides the same dependencies-first ordering guarantee the recipe relies on: forward-referenced instances are mapped before the instances that reference them. The dependency-graph dict format ({node: {predecessors}}) is identical between the two, so the graph construction is unchanged. Drop `toposort` from ifcpatch's dependencies since it is no longer used. Verified with toposort NOT installed: the Optimise recipe now runs and deduplicates correctly (IfcParseExamples_test.ifc 88 -> 63 instances, all 6 products preserved, output reopens cleanly). Generated with the assistance of an AI coding tool. Co-Authored-By: Claude Opus 4.8 --- src/ifcpatch/ifcpatch/recipes/Optimise.py | 8 ++++---- src/ifcpatch/pyproject.toml | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/ifcpatch/ifcpatch/recipes/Optimise.py b/src/ifcpatch/ifcpatch/recipes/Optimise.py index 581a89c643..3f3eb8a6fa 100644 --- a/src/ifcpatch/ifcpatch/recipes/Optimise.py +++ b/src/ifcpatch/ifcpatch/recipes/Optimise.py @@ -25,18 +25,18 @@ def _toposort(graph: dict[int, set[int]], logger: logging.Logger) -> list[int]: """Flatten a dependency graph of entity ids into dependency order. Uses igraph's C-backed topological sort when available, otherwise falls - back to the pure python toposort package with a warning. + back to the stdlib graphlib topological sort with a warning. """ try: import igraph except ImportError: logger.warning( - "igraph is not installed, falling back to the slower pure python toposort. " + "igraph is not installed, falling back to the slower stdlib graphlib. " "Install python-igraph for better performance." ) - from toposort import toposort_flatten + from graphlib import TopologicalSorter - return toposort_flatten(graph) + return list(TopologicalSorter(graph).static_order()) ids = list(graph) index = {id_: i for i, id_ in enumerate(ids)} diff --git a/src/ifcpatch/pyproject.toml b/src/ifcpatch/pyproject.toml index 22cf2ec3c5..e9f77a989f 100644 --- a/src/ifcpatch/pyproject.toml +++ b/src/ifcpatch/pyproject.toml @@ -15,7 +15,7 @@ classifiers = [ "Programming Language :: Python :: 3", "License :: OSI Approved :: GNU Lesser General Public License v3 or later (LGPLv3+)", ] -dependencies = ["ifcopenshell", "toposort", "numpy"] +dependencies = ["ifcopenshell", "numpy"] [project.optional-dependencies] advanced = [