Skip to content

Update usage of "Yields" section - #7817

Closed
eriknw wants to merge 10 commits into
networkx:mainfrom
eriknw:update_yields
Closed

eriknw wants to merge 10 commits into
networkx:mainfrom
eriknw:update_yields

Conversation

@eriknw

@eriknw eriknw commented Feb 2, 2025

Copy link
Copy Markdown
Contributor

This continues (and uses) #7258 to update usage of "Yields" section in docstrings, because several functions incorrectly use "Returns".

As we discussed last year, the docstrings were updated to not distinguish between being a generator function (a function with yield or yield from) or a function that returns an iterator, because they are functionally used the same. This PR only changes docstrings.

Using yield from iterator has the advantage of better tracebacks from exceptions within the generator. Using return iterator has the advantage that it will execute code upon first being called, so some exceptions are raised earlier (otherwise, they would raise on the first invocation of next(...)). As a general rule, I slightly prefer using yield from.

I did a first-pass update of docstrings. I updated many of them to use imperative tone (such as "Return x" instead of "Returns x"). Please make suggestions for updating (or reverting) docstrings as you feel best.

@eriknw

eriknw commented Feb 3, 2025

Copy link
Copy Markdown
Contributor Author

Also, I noticed label_propagation_communities is an outlier w.r.t. return type. It returns a dict_values object, which is an iterable and not an iterator/generator (it was a generator function). Discussion here: #4573 (comment)

What would the ideal long-term API be? Note that for accelerated backends such as nx-cugraph, we often compute everything and then return an iterator over results (but we could potentially compute results in chunks to reduce memory). For maximum flexibility w.r.t. backends, return values could be an iterable that can be safely iterated over once. An iterator satisfies this, as does an iterable (such as dict_values) and the result of a generator function. One could also imagine backends that return custom objects that have e.g. .to_dict() or to_pandas() or accessors for data on the device, etc. that satisfy the "safe to iterate over once" return type.

Also, for functions like label_propagation_communities, I wonder if it would make more sense to (eventually) return a dict.

@eriknw

eriknw commented Feb 3, 2025

Copy link
Copy Markdown
Contributor Author

I forgot that @rossbar had started this in #7288--this may result in a messy merge conflict to handle!

@rossbar

rossbar commented Feb 4, 2025

Copy link
Copy Markdown
Contributor

I forgot that @rossbar had started this in #7288 may result in a messy merge conflict to handle!

No worries! In fact, the reason that I stopped working on that is because I became less sure about how to distinguish actual Python generators from functions that return generators. I think in the original discussion in #7258 I was in favor of glossing over the difference and treating them the same (after all, the generators that are returned by functions still yield values), but now I'm not so sure. In princple, the functions that do input validation before creating a generator may surprise people when they raise before next has been called on the generator. In practice I can't think of any major consequences of this, but the fact that the standard library maintains the distinction gives me pause.

@eriknw

eriknw commented Feb 4, 2025

Copy link
Copy Markdown
Contributor Author

Yeah, this can be a little gnarly. Since we're in the universe with NetworkX backends, I would propose that we not differentiate between generator functions and functions that return iterators (for now). I don't know how risky this is to break user code--a risk for using backends in general--and I'm unsure what the preferred conventions should be for networkx. I think having language with "yield" in the docstring is more clear than "returns iterator".

I just pushed a change that makes networkx tests work when changing generator functions to functions that return iterators. This is done with a hack for now, but #7765 could make testing this super easy. I apparently forgot to test docstrings, so CI just discovered that this doctest probably isn't behaving as intended on main:

>>> G = nx.star_graph(3)
>>> index_map = {}
>>> random_path = nx.generate_random_paths(G, 3, index_map=index_map)
>>> paths_containing_node_0 = [
... random_path[path_idx] for path_idx in index_map.get(0, [])
... ]

@eriknw

eriknw commented Feb 5, 2025

Copy link
Copy Markdown
Contributor Author

p.s. it's late where I am and I will almost certainly change my mind tomorrow 😜

I'm inclined to say networkx should use generator functions when possible, so saying Yields is at least aspirational

@eriknw

eriknw commented Feb 5, 2025

Copy link
Copy Markdown
Contributor Author

Continuing this exploration, the previous commit changed all dispatchable functions that always return iterators into generator functions.

The commit before that one updated all the tests to allow these functions to either return iterators or be generator functions to give backends more flexibility.

By breaking this into two commits, we can easily cherry-pick and undo either one. Still, I think this is interesting, and I remain conflicted as to what is the "best" API for networkx.

@eriknw

eriknw commented Feb 6, 2025

Copy link
Copy Markdown
Contributor Author

The last commit edb76ff undoes 56661f3, which changed functions that return iterators into generator functions, because I'm hesitant to have this PR make changes to behavior of functions.

It doesn't seem to me that there are well-established docstring conventions for functions that return iterators. I really like the language of using "Yield" rather than saying "Return iterator", and for the "Yields" section to describe an element that is yielded rather than having "Returns" section that indicates an iterator is returned.

Perhaps for functions that return iterators, we could change the short, one-line summary at the beginning from e.g. "Yield the edge boundary of..." to "Iterate over the edge boundary of" or "Return iterator over the edge boundary of" (and you can see how concise using "Yield" is). I still prefer to use "Yields" section unless we want to consider creating a new section such as "Iterates".

@eriknw

eriknw commented Apr 10, 2026

Copy link
Copy Markdown
Contributor Author

I'm unlikely to follow up on this in a reasonable amount of time. Closing. I may cherrypick some specific improvements from this in a smaller PR in the future.

@eriknw eriknw closed this Apr 10, 2026
@rossbar

rossbar commented Apr 10, 2026

Copy link
Copy Markdown
Contributor

FWIW the ones that are truly generators (i.e. things that only yield not functions that return iterables), are unobjectionable - so if cherry-picking I'd start there!

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.

2 participants