-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Add explanations of the yield statement to common_issues.rst Fixes #17996 #18422
base: master
Are you sure you want to change the base?
Conversation
def gen() -> Generator[int]: | ||
... # error: Missing return statement | ||
|
||
This kind of mistake is common in unfinished functions (the function body is ``...``). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I'll add.
Therefore, when using ``mypy`` to check types, we need to declare the return types | ||
of such functions as Generator (or AsyncGenerator when the function is async). | ||
|
||
A common mistake is that we declared the return type of a function as |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And this is wrong: "Missing return statement" is not specific to Generator
, that's just because the return type is non-None and the body s trivial.
A reasonable function can return a Generator
or Iterator
and contain no yield
(this passes):
from collections.abc import Generator
def fn() -> Generator[int]:
return (i for i in range(5))
On the other hand, missing return statement causes a diagnostic without Generator too (except for stub files):
def fn() -> int:
... # E: Missing return statement [empty-body]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That sounds reasonable, I'll re-write these lines.
Fixes #17996 @JelleZijlstra @hauntsaninja @tilboerner Please review :)
Add explanations of the yield statement to
common_issues.rst
.No need for a test. Just modified the doc.