Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-130914: Make graphlib.TopologicalSorter.prepare() idempotent #131317

Merged
merged 6 commits into from
Mar 18, 2025

Conversation

lordmauve
Copy link
Contributor

@lordmauve lordmauve commented Mar 16, 2025

Allow calling .prepare() more than once as long as ._npassedout is 0.

Add tests, blurb and what's new.

The graphlib documentation does not mention that .prepare() may not be callled more than once and therefore does not need to be updated.

Fixes #130914


📚 Documentation preview 📚: https://cpython-previews--131317.org.readthedocs.build/

Copy link
Member

@tim-one tim-one left a comment

Choose a reason for hiding this comment

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

Good start! Some changes needed:

  • Please add your name to Misc/ACKS (I don't see it there already).

  • prepare() has two jobs, to create ._ready_nodes but also te detect cycles. I overlooked that the latter should not be skipped. So raise an error at the start if self._npassedout but protect the self._ready_nodes = ... assignment with an if self._ready_nodes is None clause.

  • The docs should change to make clear that .prepare() must be called before get_readyO, but can be called multiple times before then (and even after if get_ready() returns an empty tuple - which it will do if the graph is empty, or if all nodes are in cycles).

  • We do need a "new in ..." box. One purpose of those is to help people writing code for multiple Python versions to avoid relying on new behaviors. This needn't be fancy. Something as simple as "prepare() before could be invoked at most once, although that wasn't fully documented."

@bedevere-app
Copy link

bedevere-app bot commented Mar 16, 2025

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@tim-one tim-one self-assigned this Mar 16, 2025
@lordmauve
Copy link
Contributor Author

I have made the requested changes; please review again

@bedevere-app
Copy link

bedevere-app bot commented Mar 17, 2025

Thanks for making the requested changes!

@tim-one: please review the changes made to this pull request.

@bedevere-app bedevere-app bot requested a review from tim-one March 17, 2025 19:37
Copy link
Member

@tim-one tim-one left a comment

Choose a reason for hiding this comment

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

Looks good to me! Thank you 😄

I'll approve it, but can't merge it yet because tests are failing. Doesn't look to me that this has anything to with this patch, and expect it will "fix itself" shortly.

In the meantime, go to the bottom and click the button asking to merge the main branch into this PR. That should kick off another round ot testing.

Copy link
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

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

Small nits but otherwise LGTM.


* 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
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`.)

Comment on lines +154 to +156
for attempt in range(1, 4):
with self.assertRaises(graphlib.CycleError, msg=f"{attempt=}"):
ts.prepare()
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
for attempt in range(1, 4):
with self.assertRaises(graphlib.CycleError, msg=f"{attempt=}"):
ts.prepare()
for attempt in range(1, 4):
with self.subTest(attempt=attempt):
self.assertRaises(graphlib.CycleError, ts.prepare)

You can also use subTest() if you want.

@tim-one
Copy link
Member

tim-one commented Mar 18, 2025

OK, I kicked off another round of testing myself, and they all pass now. About to merge!

@tim-one tim-one merged commit c1b42db into python:main Mar 18, 2025
39 checks passed
sebbASF added a commit to sebbASF/cpython that referenced this pull request Mar 18, 2025
colesbury pushed a commit to colesbury/cpython that referenced this pull request Mar 20, 2025
…python#131317)

Closes python#130914: Make graphlib.TopologicalSorter.prepare() idempotent

Relax the rules so that `.prepare()` can be called multiple times, provided that no work has been passed out by `.get_ready()` yet.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

graphlib.TopologicalSorter.prepare() should be idempotent
3 participants