Repository navigation
Improve docs for all_neighbors() - #8166
Conversation
- Add comprehensive examples for both undirected and directed graphs - Add error handling documentation (NetworkXError) - Add detailed Notes section explaining API consistency benefits - Address issue networkx#8044 through documentation improvements instead of API changes - Clarify when and why to use nx.all_neighbors() vs class methods
|
|
||
| Notes | ||
| ----- | ||
| For undirected graphs, this function is equivalent to `G.neighbors(node)`. |
There was a problem hiding this comment.
Inline literals should be wrapped in `` instead of single-backticks in rST.
| For undirected graphs, this function is equivalent to `G.neighbors(node)`. | |
| For undirected graphs, this function is equivalent to ``G.neighbors(node)``. |
This applies to the other instances in this section too!
| **Related methods:** | ||
| - `G.neighbors(n)`: Works for both Graph and DiGraph (returns successors for directed) | ||
| - `G[n]`: Shorthand equivalent to `G.neighbors(n)` for both graph types | ||
| - `G.pred[u]`: Returns predecessors for directed graphs only |
There was a problem hiding this comment.
NetworkX uses numpydoc-style docstrings, so I think this is best reorganized as a See Also section.
- Change single backticks to double backticks for inline literals in rST - Move Related methods section to proper See Also section with numpydoc formatting - Address maintainer feedback on PR for issue networkx#8044
|
@rossbar I have addressed the formatting concerns. |
- Move G[n] and G.pred[u] syntax examples to Notes section - Keep only valid function references in See Also section - Fix CircleCI documentation build error with numpydoc parser
all_neighbors()
|
You should use the pre-commit hooks stored in the repo (as described in the contributor guide). The single-tick vs double-tick is subtle in that code should use double-ticks while symbols from code that are identifiable as parameters or functions should use single-tick to get the link in the documentation to that parameter or function. That's hard for the linters to distinguish. As for sections, I don't think linting can tell you to change a "related methods" section to a "See Also" section... But perhaps I'm just not aware of the latest linting??? |
This has been proposed before and the general consensus was that the docstring linting tools are extremely noisy, see e.g. #7554 |
| For undirected graphs, this function is equivalent to ``G.neighbors(node)``. | ||
| For directed graphs, this function returns both predecessors and successors, | ||
| which may include duplicates if a node is both a predecessor and successor | ||
| (e.g., in bidirectional edges or self-loops). |
There was a problem hiding this comment.
These two sentences make more sense in the example section providing context for the two different examples. I.e., the first sentence can go in front of the first example, the second in front of the second.
| which may include duplicates if a node is both a predecessor and successor | ||
| (e.g., in bidirectional edges or self-loops). | ||
|
|
||
| This function provides a consistent interface for accessing neighbors |
There was a problem hiding this comment.
| This function provides a consistent interface for accessing neighbors | |
| This function provides a consistent interface for accessing all neighbors |
| Alternative access methods: | ||
| - ``G[n]`` : Shorthand equivalent to ``G.neighbors(n)`` for both graph types | ||
| - ``G.pred[u]`` : Returns predecessors for directed graphs only | ||
|
|
There was a problem hiding this comment.
I'd vote to remove these lines, they're an odd fit for the all_neighbors docstring.
| Alternative access methods: | |
| - ``G[n]`` : Shorthand equivalent to ``G.neighbors(n)`` for both graph types | |
| - ``G.pred[u]`` : Returns predecessors for directed graphs only |
|
|
||
| See Also | ||
| -------- | ||
| neighbors : Returns successors for both Graph and DiGraph |
There was a problem hiding this comment.
These should link back to the specific methods, e.g. something like
| neighbors : Returns successors for both Graph and DiGraph | |
| :meth:`G.neighbors <networkx.classes.Graph.neighbors>` : Returns successors for both Graph and DiGraph |
Co-authored-by: Dan Schult <[email protected]>
rossbar
left a comment
There was a problem hiding this comment.
I took the liberty of pushing up the remaining review suggestions - this LGTM, thanks @Yasserelhaddar !
dschult
left a comment
There was a problem hiding this comment.
What's the current tradition about Yields vs Returns in the docs?
The descriptions in "See Also" section also use the word returns instead of yields.
I'm OK with leaving that for a separate PR too.
Good point - I'm personally still not sure what to do with the pattern of "fn that returns an iterator" as is the case here. It's technically a function in the sense that it promptly executes and returns, but from a user's perspective the result is an iterator regardless of whether it was defined as a generator or returned from a procedure that creates iterators. At various times I've convinced myself that both sides of the argument are correct (to My vote is to punt on it in this PR! |
* Improve all_neighbors function documentation (fixes networkx#8044) - Add comprehensive examples for both undirected and directed graphs - Add error handling documentation (NetworkXError) - Add detailed Notes section explaining API consistency benefits - Address issue networkx#8044 through documentation improvements instead of API changes - Clarify when and why to use nx.all_neighbors() vs class methods * Address reviewer feedback: add cross-references to related methods * docs: fix rST formatting and numpydoc style in all_neighbors docstring - Change single backticks to double backticks for inline literals in rST - Move Related methods section to proper See Also section with numpydoc formatting - Address maintainer feedback on PR for issue networkx#8044 * docs: fix numpydoc See Also section parsing error - Move G[n] and G.pred[u] syntax examples to Notes section - Keep only valid function references in See Also section - Fix CircleCI documentation build error with numpydoc parser * Update networkx/classes/function.py Co-authored-by: Dan Schult <[email protected]> * Add remaining review comments. * One more numpydoc fixup. --------- Co-authored-by: Yasserelhaddar <[email protected]> Co-authored-by: Yasser El Haddar <[email protected]> Co-authored-by: Ross Barnowski <[email protected]> Co-authored-by: Dan Schult <[email protected]>
Pull Request: Improve all_neighbors function documentation (fixes #8044)
Description
This PR addresses Issue #8044 by improving the documentation for the
all_neighbors()function instead of adding new methods to graph classes, as suggested by the maintainers.Problem
The original issue reported that
G.all_neighbors(n)fails with AttributeError on undirected graphs, whilenx.all_neighbors(G, n)works correctly. The maintainers provided feedback that they prefer documentation improvements over adding new methods to the graph classes.Solution
Enhanced the documentation for the existing
nx.all_neighbors()function innetworkx/classes/function.pywith:nx.all_neighbors()vs class methodsChanges Made
File Modified:
networkx/classes/function.pyEnhanced
all_neighbors()function documentation with:Examples Section:
Error Handling Documentation:
Comprehensive Notes Section:
Testing
Benefits
nx.all_neighbors()for consistent behavior across graph typesRelated Issue
Closes #8044
Type of Change
Branch:
fix-8044-add-all-neighbors-methodBase Branch:
mainRepository:
yasserelhaddar/networkx→networkx/networkx