Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,15 @@ getopt
* Add support for returning intermixed options and non-option arguments in order.
(Contributed by Serhiy Storchaka in :gh:`126390`.)


graphlib
--------

* Allow :meth:`graphlib.TopologicalSorter.prepare` to be called more than once
as long as sorting has not started.
(Contributed by Daniel Pope in :gh:`130914`)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(Contributed by Daniel Pope in :gh:`130914`)
(Contributed by Daniel Pope in :gh:`130914`.)



http
----

Expand Down
4 changes: 3 additions & 1 deletion Lib/graphlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ def prepare(self):
therefore no more nodes can be added using "add".
"""
if self._ready_nodes is not None:
raise ValueError("cannot prepare() more than once")
if self._npassedout > 0:
raise ValueError("cannot prepare() after starting sort")
return

self._ready_nodes = [
i.node for i in self._node2info.values() if i.npredecessors == 0
Expand Down
8 changes: 7 additions & 1 deletion Lib/test/test_graphlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,13 @@ def test_calls_before_prepare(self):
def test_prepare_multiple_times(self):
ts = graphlib.TopologicalSorter()
ts.prepare()
with self.assertRaisesRegex(ValueError, r"cannot prepare\(\) more than once"):
ts.prepare()

def test_prepare_after_pass_out(self):
ts = graphlib.TopologicalSorter({'a': 'bc'})
ts.prepare()
self.assertEqual(set(ts.get_ready()), {'b', 'c'})
with self.assertRaisesRegex(ValueError, r"cannot prepare\(\) after starting sort"):
ts.prepare()

def test_invalid_nodes_in_done(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Allow :meth:`graphlib.TopologicalSorter.prepare` to be called more than once
as long as sorting has not started.
Patch by Daniel Pope.