Repository navigation
Conversation
|
Also, I noticed What would the ideal long-term API be? Note that for accelerated backends such as Also, for functions like |
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 |
|
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: networkx/networkx/algorithms/similarity.py Lines 1712 to 1717 in 34130d7 |
|
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 |
|
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. |
|
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. |
|
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. |
|
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! |
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
yieldoryield from) or a function that returns an iterator, because they are functionally used the same. This PR only changes docstrings.Using
yield from iteratorhas the advantage of better tracebacks from exceptions within the generator. Usingreturn iteratorhas 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 ofnext(...)). As a general rule, I slightly prefer usingyield 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.