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 <noreply@anthropic.com>
This commit is contained in:
Petru Conduraru
2026-07-12 08:00:55 +03:00
committed by Dion Moult
parent 81a42cec1a
commit 382f5e0c21
2 changed files with 5 additions and 5 deletions
+4 -4
View File
@@ -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)}
+1 -1
View File
@@ -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 = [