Skip to content

Improve docs for all_neighbors() - #8166

Merged
dschult merged 8 commits into
networkx:mainfrom
Yasserelhaddar:fix-8044-documentation-improvement
Aug 30, 2025
Merged

dschult merged 8 commits into
networkx:mainfrom
Yasserelhaddar:fix-8044-documentation-improvement

Conversation

@Yasserelhaddar

Copy link
Copy Markdown
Contributor

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, while nx.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 in networkx/classes/function.py with:

  • Comprehensive examples for both undirected and directed graphs
  • Error handling documentation (NetworkXError)
  • Detailed Notes section explaining:
    • How the function works for different graph types
    • Why it provides a consistent interface
    • When to use nx.all_neighbors() vs class methods
  • Clear explanation of the difference between undirected and directed behavior

Changes Made

File Modified: networkx/classes/function.py

Enhanced all_neighbors() function documentation with:

  1. Examples Section:

    >>> G = nx.path_graph(4)  # or DiGraph, MultiGraph, MultiDiGraph, etc
    >>> list(nx.all_neighbors(G, 1))
    [0, 2]
    >>> DG = nx.DiGraph([(0, 1), (1, 2), (2, 1)])
    >>> list(nx.all_neighbors(DG, 1))
    [0, 2, 2]
  2. Error Handling Documentation:

    Raises
    ------
    NetworkXError
        If the node is not in the graph.
  3. Comprehensive Notes Section:

    • Explains behavior for undirected vs directed graphs
    • Clarifies when duplicates may occur
    • Highlights the consistent interface benefit
    • Compares with class methods

Testing

  • All existing tests pass
  • Documentation improvements are purely additive
  • No functional changes to the code

Benefits

  1. Better User Experience: Clear examples help users understand when and how to use the function
  2. Consistent API: Emphasizes the benefit of using nx.all_neighbors() for consistent behavior across graph types
  3. Error Prevention: Documents potential errors and their causes
  4. Maintainer Alignment: Follows maintainer feedback to improve documentation rather than add methods

Related Issue

Closes #8044

Type of Change

  • Documentation improvement
  • Bug fix
  • New feature
  • Performance improvement
  • Code refactoring

Branch: fix-8044-add-all-neighbors-method
Base Branch: main
Repository: yasserelhaddar/networkx → networkx/networkx

YasserElHaddar16 and others added 2 commits July 27, 2025 15:18
- 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

@amcandio amcandio left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good to me

Comment thread networkx/classes/function.py Resolved
Comment thread networkx/classes/function.py Outdated

Notes
-----
For undirected graphs, this function is equivalent to `G.neighbors(node)`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Inline literals should be wrapped in `` instead of single-backticks in rST.

Suggested change
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!

Comment thread networkx/classes/function.py Outdated
Comment on lines +1006 to +1009
**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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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
@Yasserelhaddar

Copy link
Copy Markdown
Contributor Author

@rossbar I have addressed the formatting concerns.
I am wondering if we can use pre-commit hooks like: pydocstyle and darglint to ensure this doctoring style alignment:

- repo: https://github.com/pycqa/pydocstyle
    rev: 6.3.0
    hooks:
      - id: pydocstyle
        args: [--convention=numpy]

- repo: https://github.com/terrencepreilly/darglint
    rev: v1.8.1
    hooks:
      - id: darglint
        args: [--docstring-style=numpy]

- 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
@dschult dschult changed the title Fix 8044 documentation improvement Improve docs for all_neighbors() Jul 30, 2025
@dschult

dschult commented Jul 30, 2025

Copy link
Copy Markdown
Member

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???

@rossbar

rossbar commented Jul 30, 2025

Copy link
Copy Markdown
Contributor

I am wondering if we can use pre-commit hooks like: pydocstyle and darglint to ensure this doctoring style alignment:

This has been proposed before and the general consensus was that the docstring linting tools are extremely noisy, see e.g. #7554

Comment thread networkx/classes/function.py Outdated
Comment on lines +996 to +999
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).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment thread networkx/classes/function.py Outdated
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
This function provides a consistent interface for accessing neighbors
This function provides a consistent interface for accessing all neighbors

Comment thread networkx/classes/function.py Outdated
Comment on lines +1006 to +1009
Alternative access methods:
- ``G[n]`` : Shorthand equivalent to ``G.neighbors(n)`` for both graph types
- ``G.pred[u]`` : Returns predecessors for directed graphs only

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'd vote to remove these lines, they're an odd fit for the all_neighbors docstring.

Suggested change
Alternative access methods:
- ``G[n]`` : Shorthand equivalent to ``G.neighbors(n)`` for both graph types
- ``G.pred[u]`` : Returns predecessors for directed graphs only

Comment thread networkx/classes/function.py Outdated

See Also
--------
neighbors : Returns successors for both Graph and DiGraph

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

These should link back to the specific methods, e.g. something like

Suggested change
neighbors : Returns successors for both Graph and DiGraph
:meth:`G.neighbors <networkx.classes.Graph.neighbors>` : Returns successors for both Graph and DiGraph

@Yasserelhaddar

Copy link
Copy Markdown
Contributor Author

@rossbar and @dschult , I see different suggestions here. Should I:

  • Keep the alternative access methods section with G[n] and G.pred[u]?
  • Or remove it to keep the docstring focused?

I'm happy to implement whichever approach you prefer.

Comment thread networkx/classes/function.py Resolved

@rossbar rossbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I took the liberty of pushing up the remaining review suggestions - this LGTM, thanks @Yasserelhaddar !

@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.

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.

@rossbar

rossbar commented Aug 29, 2025

Copy link
Copy Markdown
Contributor

What's the current tradition about Yields vs Returns in the docs?

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 Yields or not to Yields) 🤷

My vote is to punt on it in this PR!

@dschult
dschult merged commit dc6de81 into networkx:main Aug 30, 2025
@jarrodmillman jarrodmillman added this to the 3.6 milestone Aug 30, 2025
alfred-mikhael pushed a commit to alfred-mikhael/networkx that referenced this pull request Sep 3, 2025
* 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]>
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.

An undirected 'Graph' object has no attribute 'all_neighbors', but the docs imply it should.

6 participants