-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
Description
Feature or enhancement
Proposal:
Proposed Behaviour
TopologicalSorter.prepare() should be idempotent such that calling it repeatedly without draining anything is not an error:
>>> import graphlib
>>> ts = graphlib.TopologicalSorter()
>>> ts.prepare()
>>> ts.prepare()If you have called .get_ready()/.done() then calling prepare() is probably a programming error and this would raise:
>>> import graphlib
>>> ts = graphlib.TopologicalSorter()
>>> ts.prepare()
>>> ts.get_ready()
()
>>> ts.prepare()
Traceback (most recent call last):
...
ValueError: cannot prepare() after mutating prepared sorterRationale
TopologicalSorter.prepare() raises an exception if you call it twice:
>>> import graphlib
>>> ts = graphlib.TopologicalSorter()
>>> ts.prepare()
>>> ts.prepare()
Traceback (most recent call last):
File "<python-input-3>", line 1, in <module>
ts.prepare()
~~~~~~~~~~^^
File "/home/mauve/.local/share/ext-python/python-3.13.0.82/lib/python3.13/graphlib.py", line 95, in prepare
raise ValueError("cannot prepare() more than once")
ValueError: cannot prepare() more than onceThis is rather unfortunate because if you return a TopologicalSorter that is prepared:
def get_sorter(targets) -> TopologicalSorter[str]:
"""Get a TopologicalSorter that pursues the given targets."""
deps = filter_deps(load_deps(), targets)
ts = TopologicalSorter(deps)
ts.prepare()
return tsbecause then you cannot run .static_order() on it:
>>> get_sorter().static_order()
Traceback (most recent call last):
...
ValueError: cannot prepare() more than oncewhile if you don't prepare(), you then require the caller to do it, meaning the function that populates and returns a TopologicalSorter didn't leave it in a prepared state. It seems appropriate for such a function to call prepare() in order to leave the TopologicalSorter ready to iterate and also closed for the addition of new nodes/predecessors.
Therefore I think prepare() should be idempotent.
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
This is related to #91301 which discusses removing TopologicalSorter.prepare() entirely. Doing so would bypass this issue.