-
Notifications
You must be signed in to change notification settings - Fork 164
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
python BFS and DFS search documentation improvements #1361
Comments
In general I think documentation improvements are always welcome. I did suggest the idea of having the pseudocode in Python in #489 but the consensus was that the existing pseudocode was good enough. I will start by saying that the terminology used is the same as the one in CLRS. The book did not invent it but it popularized it for sure. We should probably cite Chapter 20 "Elementary Graph Algorithms" from the 4th edition. I will group the answer by: Panicking
Modifying the graphAnswering about modifying the graph while iterating. Modifying the input while calling I am ok with documenting Type annotationsI can take that one myself and clean it up after #1353 is merged. There are some improvements from #1352 as well. Once I get Matthew to review my pending PRs it will be easier to work on that. Documentation improvementsYou can send the improvements. I don't mind changing the pseudocode to Python as long as it is roughly the same. It shouldn't be a full-fledged implementation but something good enough to give a guide on how to use the code. We should also document that the universal function Another idea is to add more examples to the documentation, maybe with a tutorial on how to use the visitors. |
Thanks for the informative response! I'll get to work on the PR on the documentations in a couple of days probably. Do you have a plan on what to replace the panic with? If you have a concrete exception type in mind I can put it in the documentation. |
I think RuntimeError is fine in the place of a panic. I will think more carefully about it but that one is a good umbrella error for things we don’t want to handle |
So I tried to add the annotations in #1362 and the results was that I will probably need to add an overload with https://docs.python.org/3/library/typing.html#typing.Never for the I never thought argument ordering would bite us back so much! |
Overloading can be tricky. There are a lot of sharp corners for complex signatures. I'll take a look at your commit with the overloading. It is considered bad practice to use From typing import Never
def neverreturns(): -> Never:
assert False
neverreturns() # no errors by mypy
1 + 'hello' # this isn't type checked The idea is that Python simply does not have a good alternative to rust's |
I agree it is bad practice but I need to cover the all None case. I’d rather not tell users the all None case is available at all. But our tests catch that “mistake” |
I did some trials and I believe the following should work @overload
def digraph_bfs_search(
graph: PyDiGraph, source: Sequence[int] | None=..., *, visitor: _BFSVisitor,
) -> None: ...
@overload
def digraph_bfs_search(
graph: PyDiGraph, source: Sequence[int] | None, visitor: _BFSVisitor,
) -> None: ...
@overload
def digraph_bfs_search(
graph: PyDiGraph, source: Never, visitor: None = ...,
) -> None: ... This passes both mypy and stubtest (on minimal examples I recreated). Notice that I put the Never in the argument for source, so type checkers are instructed to forbid this overload signature. |
What is the expected enhancement?
I find the documentations for the python visit functions (
dfs_search
etc.) confusing, and I think all users can benefit from some additions to them.It might be that my sparse prior knowledge in graph algorithms, but I found the documentation confusing with all the colors (WHITE, GRAY, BLACK) mentioned without context to the BFSVisitor events, or the term "forward or cross edge" which I didn't know.
In the end, I read the rust implementation to understand the algorithm. The code is clearly well thought out and it would be an injustice to keep not let it shine in the documentation as well.
I suggest the following changes:
rustworkx.dfs_search
docstring: mention that thegraph
can be aPyDiGraph
as well.source
fromList[int]
toSequence[int] | None
with defaultNone
.rustworkx.pyi
) for the different visit functions so thatvisitor=None
is not accepted by static type checkers. This can be achieved with overloads:RuntimeError
.". This way the user can know which exception type he should catch in such cases.PruneSearch
in afinish_vertex
method raises an exception. I checked and this behavior is documented inrustworkx-core
where the function call panics in such cases.PanicException
iffinish_vertex
raisesPrunceSearch
. This exception isn't caught byexcept Exception
and user cannot easily narrow it withexcept PanicException
sincePanicException
is inpyo3_runtime
module, which is not site-packages usually and cannot be easily imported.I only looked at the
bfs_search
anddfs_search
which I use, but I suspectdijkstra_search
also could benefit from similar changes.If you agree, I can open a PR with the agreed upon changes and also look into the Dijkstra case.
Regrading the pseudo-code elaborations. Here is an option: replacing the current pseudo-code with python alternative (python is already kinda pseudo-codish). I wrote python alternatives while trying to understand the rust code. Here they are after removing the control flow for the
PruneSearch
andStopSearch
(I can reproduce the control flow if desired):and
The text was updated successfully, but these errors were encountered: