python / cpython

The Python programming language
https://www.python.org
Other
63.62k stars 30.47k forks source link

Provide a simple function interface for topological sort in graphlib #125181

Open erezsh opened 1 month ago

erezsh commented 1 month ago

Feature or enhancement

Proposal:

The graphlib interface requires 5 function calls in order to get the result of a topological sort.

I believe that the most common user will most likely only need a single function call, i.e.

def topological_sort(graph: dict) -> Iterable[set]: ...

I propose adding this small utility function to graphlib.

Note: there is static_order(), which reduces it to 2 function calls, but it flattens the result, which is a bad default.

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:

No response

tomasr8 commented 1 month ago

Note: there is static_order(), which reduces it to 2 function calls, but it flattens the result, which is a bad default.

I don't know if I agree with it being a bad default, but perhaps it'd be possible to add a flag to return the groups/levels instead? On the other hand, I like keeping the static_order function simple and just using prepare/get_ready manually when you need the groups.

erezsh commented 1 month ago

I say it's a bad default because it's simple to flatten the groups into the result of static_order, but impossible to reverse it.

just using prepare/get_ready

It's overly complicated, with very little benefit for the common use-case.