-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Functions yield
ing wrongly report errors about Return:
being missing
#15
Comments
Hi @leandro-lucarella-frequenz , I think the more accurate return type of this >>> from typing import Iterator, Generator
>>> issubclass(Generator, Iterator)
True
>>> issubclass(Iterator, Generator)
False pydoclint checks the return annotation of methods/functions, and if the return annotation is |
This goes against the google style. The google-style says
https://google.github.io/styleguide/pyguide.html#294-decision |
Also Python typing documentation says that
https://docs.python.org/3/library/typing.html#typing.Generator |
Hi @leandro-lucarella-frequenz , let me clarify this a bit more. I might have been unclear before. The reason why we are seeing this violation ("DOC201: Method Currently, only when pydoclint sees a return annotation of I could make a small code change so that when pydoclint sees from typing import Any, Iterator, Tuple, List
def zip_lists(list1: List[Any], list2: List[Any]) -> Iterator[Tuple[Any, Any]]:
return zip(list1, list2) Look at this function If I implemented the change I suggested above, this function would fall through the crack and become unchecked by pydoclint. Do you have any suggestions on how to solve this dilemma? |
Hi @jsh9 , thanks for the clarification. Yes! I do have a suggestion for your dilemma :) I think what That said, I'm not sure how complicated implementing this could be, but BTW, same applies to |
Thanks! I don't think this would be very difficult to implement. Btw, how do you suggest that I deal with functions that both yield something and return something? Shall we require a "Returns:" section in the docstring if there's a return statement? It's probably not the best practice but I've seen code that does this. Btw I asked ChatGPT about this, and it gave me some concrete examples: QuestionIn Python, can I technically both return something and yield something in the same function? AnswerYes, in Python, you can technically include both
When you use When a Here's an example: def func():
yield 1
yield 2
return 3
gen = func() # This returns a generator object, it doesn't run the function's code
print(next(gen)) # This prints 1, the first value yielded by the generator
print(next(gen)) # This prints 2, the second value yielded by the generator
print(next(gen)) # This raises a StopIteration exception with 3 as its argument In the last line, trying to get the next value from the generator causes the try:
print(next(gen))
except StopIteration as e:
print(e.value) # This prints 3, the value returned by the function In general, it's best to only use |
Require both |
I think you should document what the code does, if it does both, document both. |
Hi @leandro-lucarella-frequenz , I fixed this issue. It's now included in the newest release (v0.0.8). Please feel free to take a look! |
Thanks! I'm a bit backed up, so I might not be able to try it this week. BTW, I'm using a new gh handle but I'm the same guy :D |
Hi @jsh9, sorry for the radio silence for so long, your support didn't go unnoticed at all, I just really needed to take on some other high-priority topics, but I think I will have time this week to resume my efforts to migrate from darglint to pydoclint. |
OK, I was able to test this again with some of our (open source) projects and it works great! Thanks a lot, it looks like the project had a lot of improvements. We are still hitting 2 blockers, hopefully that's all and we can finally switch: We are very eager for the switch, |
When functions
yield
instead of return, aYield:
should be used in the docstring (at least in google-style) instead ofReturn:
, butpydoclint
complains about a missingReturn:
.For example this code: https://github.com/frequenz-floss/frequenz-sdk-python/blob/776352c0e9383506a778751560a3e21e91e2d6fd/src/frequenz/sdk/timeseries/_base_types.py#L52-L60
Fails with:
When running:
pydoclint --style google -aid True -th False src/
(forpydoclint
0.0.6)The text was updated successfully, but these errors were encountered: