Add explanations of the yield statement to common_issues.rst Fixes #17996#18422
Open
keainye wants to merge 1 commit intopython:masterfrom
Open
Add explanations of the yield statement to common_issues.rst Fixes #17996#18422keainye wants to merge 1 commit intopython:masterfrom
keainye wants to merge 1 commit intopython:masterfrom
Conversation
sterliakov
suggested changes
Jan 13, 2025
| def gen() -> Generator[int]: | ||
| ... # error: Missing return statement | ||
|
|
||
| This kind of mistake is common in unfinished functions (the function body is ``...``). |
Collaborator
| 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 |
Collaborator
There was a problem hiding this comment.
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]
Author
There was a problem hiding this comment.
That sounds reasonable, I'll re-write these lines.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.