Skip to content

Minor updates to single_source_shortest_path_length docstring - #7637

Merged
Schefflera-Arboricola merged 4 commits into
networkx:mainfrom
rossbar:sp-docstrings-tlc
Dec 17, 2024
Merged

Schefflera-Arboricola merged 4 commits into
networkx:mainfrom
rossbar:sp-docstrings-tlc

Conversation

@rossbar

@rossbar rossbar commented Sep 17, 2024

Copy link
Copy Markdown
Contributor
  • Wrap parameters in single quotes
  • Simplify docstring example
  • Make see-also section linkable and add dijkstra + bf
  • Add 1-sentence description to each See-Also Entry

 * Wrap parameters in single quotes
 * Simplify docstring example
 * Make see-also section linkable and add dijkstra + bf
:func:`~networkx.algorithms.shortest_paths.weighted.single_source_dijkstra_path_length` :
Shortest weighted path length from source with Dijkstra algorithm.
:func:`~networkx.algorithms.shortest_paths.weighted.single_source_bellman_ford_path_length` :
Shorttest weighted path length from source with Bellman-Ford algorithm.

@dschult dschult Sep 18, 2024 •

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.

I thought the See Also section automagically made links. Is that not true for this case? Or in general now?

Also, here's a typo fix.

Suggested change
Shorttest weighted path length from source with Bellman-Ford algorithm.
Shortest weighted path length from source with Bellman-Ford algorithm.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I thought the See Also section automagically made links. Is that no true for this case? Or in general now?

There's a catch - it only does so functions that are defined in the same module. For example, even though shortest_path_length is obviously related, because it is defined in networkx.algorithms.shortest_paths.generic instead of unweighted, the link doesn't work.

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.

Can we help out sphinx by importing the "See Also" functions into the module? They wouldn't be in __all__ so I think the namespace wouldn't get polluted beyond that module.

I'm worried that these links are fairly hard to read in text form. Also, in the text form, they emphasize (due to length) the location of the source in the source tree rather than the name of the function. It is a depth-4, 82-char pointer to the function. Hmm... Could sphinx use the namespace beyond the current module to look up the See Also info? There must be a reason it uses the source tree instead.

What do you think of importing the see-also functions into each module? Are there downsides to doing that?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Can we help out sphinx by importing the "See Also" functions into the module? They wouldn't be in all so I think the namespace wouldn't get polluted beyond that module.

I'm not sure how this actually works in practice. I suspect that this wouldn't help, as my feeling is that the reference mapping (e.g. objects.inv) that sphinx builds is keyed by where the docstrings are found in the source; i.e. this depends only on source traversal rather than import/run-time structure.

I don't know this for sure though, so I will experiment! Another thing which may work is injecting .. currentmodule:: directives in the see-also section. It doesn't help with aesthetics much, but it would at least eliminate the "explicit-path-in-plain-text" problem. To be clear, I'm not sure this would work either - I'll add it to the experiment list. Ultimately, if we can find a solution like this (and one doesn't already exist in autodoc that I"m just unaware of), then it'd definitely IMO be worth upstreaming into numpydoc or some other sphinx extension to handle this automatically.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok, I've tried a few things: the .. currentmodule idea is DOA, it breaks the See Also parsing.

However, one thing that does work is to use an :any: role (see 3a9d2ae). This is closer to the intended behavior, i.e. link to the relevant docstring without all of the path cruft. It's not perfect because having to explicitly specify the :any: role is still a visual wart in the raw docstring, though it's much better than the :func: role requiring the full path in the source tree.

This feels like something that could be made configurable in numpydoc, i.e. allow users to configure a default role for see-also entries.

Comment on lines +48 to +53
:func:`~networkx.algorithms.shortest_paths.generic.shortest_path_length` :
Shortest path length with specifiable source, target, and weight.
:func:`~networkx.algorithms.shortest_paths.weighted.single_source_dijkstra_path_length` :
Shortest weighted path length from source with Dijkstra algorithm.
:func:`~networkx.algorithms.shortest_paths.weighted.single_source_bellman_ford_path_length` :
Shortest weighted path length from source with Bellman-Ford algorithm.

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.

Do we need single backticks in these see-also descriptions too? And should the indent be 4 chars?

And would it be better to describe when they should use djikstra/bellman-ford rather than just give the name? (And maybe it is best to use the single line description from the function itself for consistency across "See-also" sections. Sphinx does that for automodule... but I guess not for See Also)

Suggested change
:func:`~networkx.algorithms.shortest_paths.generic.shortest_path_length` :
Shortest path length with specifiable source, target, and weight.
:func:`~networkx.algorithms.shortest_paths.weighted.single_source_dijkstra_path_length` :
Shortest weighted path length from source with Dijkstra algorithm.
:func:`~networkx.algorithms.shortest_paths.weighted.single_source_bellman_ford_path_length` :
Shortest weighted path length from source with Bellman-Ford algorithm.
:func:`~networkx.algorithms.shortest_paths.generic.shortest_path_length` :
Shortest path length with specifiable source, target, and weight.
:func:`~networkx.algorithms.shortest_paths.weighted.single_source_dijkstra_path_length` :
Shortest weighted path length from `source` for positive weights.
:func:`~networkx.algorithms.shortest_paths.weighted.single_source_bellman_ford_path_length` :
Shortest weighted path length from `source` for possibly negative weights.

To be clear, these nit-picks aren't needed for this PR. But I bring them up because we're doing a deep-dive in prep for rewriting all these doc_strings.

@rossbar rossbar Sep 19, 2024 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Do we need single backticks in these see-also descriptions too?

Yep - that was an oversight on my part - good catch!

Actually, thinking about this more, this case is a bit ambiguous. The rule is "wrap function parameters in single backticks" (more or less). However, in this case we're referring to the "source" node in the context of another function. Another way to think about this is - if parameter linking worked the way it was supposed to, then clicking on the source here in the html docstring would take you back up to an anchor link on the source parameter for the single_source_shortest_path_length function. Is that correct?... personally I could see either way!

And should the indent be 4 chars?

Aside from style concerns it doesn't matter. I'm used to 3-spaces for rst, which I think is the most common level of indentation (and which AFAIK is a convention stemming from the fact that directives start with .. , and folks like to align text with directive names). However, 4 is also fine - I don't believe we have/enforce any style rules on this front, but I'd be in favor of doing so (automatically, that is!)

And would it be better to describe when they should use djikstra/bellman-ford rather than just give the name?

Absolutely - I just wasn't confident about when one might be preferred over 🙃

@dschult dschult left a comment

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.

This looks ready to go.
There are some nice ideas in the conversation (like using :any: and linking of arguments to the function they are defined for).

Is there anything else left to do here @rossbar ?

@Schefflera-Arboricola
Schefflera-Arboricola merged commit ed60d79 into networkx:main Dec 17, 2024
@jarrodmillman jarrodmillman added this to the 3.5 milestone Dec 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

4 participants