-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Make coroutine function return type more specific #5052
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
Merged
ilevkivskyi
merged 8 commits into
python:master
from
AndreLouisCaron:fix-coroutine-function-return-type
May 17, 2018
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e667a6e
Change the return type of coroutine functions to coroutine object
AndreLouisCaron 4857d5d
Prevent coroutine functions from being classified as generators
AndreLouisCaron f11b14e
Allow implicit return for coroutine functions that return `None`
AndreLouisCaron 2d4c212
Fix return type for coroutine functions decorated with @coroutine
AndreLouisCaron d7977b0
Fix detection of await expression on direct coroutine function call
AndreLouisCaron 309cbf2
Fix regression after change of coroutine function return type
AndreLouisCaron 9623975
Fix position of return type in `Coroutine`
AndreLouisCaron d3ac484
Fix issues raised in code review
AndreLouisCaron File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -578,6 +578,13 @@ def get_generator_receive_type(self, return_type: Type, is_coroutine: bool) -> T | |
# values. IOW, tc is None. | ||
return NoneTyp() | ||
|
||
def get_coroutine_return_type(self, return_type: Type) -> Type: | ||
if isinstance(return_type, AnyType): | ||
return AnyType(TypeOfAny.from_another_any, source_any=return_type) | ||
assert isinstance(return_type, Instance), "Should only be called on coroutine functions." | ||
# Note: return type is the 3rd type parameter of Coroutine. | ||
return return_type.args[2] | ||
|
||
def get_generator_return_type(self, return_type: Type, is_coroutine: bool) -> Type: | ||
"""Given the declared return type of a generator (t), return the type it returns (tr).""" | ||
if isinstance(return_type, AnyType): | ||
|
@@ -756,7 +763,10 @@ def check_func_def(self, defn: FuncItem, typ: CallableType, name: Optional[str]) | |
c = defn.is_coroutine | ||
ty = self.get_generator_yield_type(t, c) | ||
tc = self.get_generator_receive_type(t, c) | ||
tr = self.get_generator_return_type(t, c) | ||
if c: | ||
tr = self.get_coroutine_return_type(t) | ||
else: | ||
tr = self.get_generator_return_type(t, c) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like the symmetry between the branches here. 👍 |
||
ret_type = self.named_generic_type('typing.AwaitableGenerator', | ||
[ty, tc, tr, t]) | ||
typ = typ.copy_modified(ret_type=ret_type) | ||
|
@@ -841,6 +851,8 @@ def check_func_def(self, defn: FuncItem, typ: CallableType, name: Optional[str]) | |
is_named_instance(self.return_types[-1], 'typing.AwaitableGenerator')): | ||
return_type = self.get_generator_return_type(self.return_types[-1], | ||
defn.is_coroutine) | ||
elif defn.is_coroutine: | ||
return_type = self.get_coroutine_return_type(self.return_types[-1]) | ||
else: | ||
return_type = self.return_types[-1] | ||
|
||
|
@@ -878,7 +890,7 @@ def is_unannotated_any(t: Type) -> bool: | |
if is_unannotated_any(ret_type): | ||
self.fail(messages.RETURN_TYPE_EXPECTED, fdef) | ||
elif (fdef.is_coroutine and isinstance(ret_type, Instance) and | ||
is_unannotated_any(ret_type.args[0])): | ||
is_unannotated_any(self.get_coroutine_return_type(ret_type))): | ||
self.fail(messages.RETURN_TYPE_EXPECTED, fdef) | ||
if any(is_unannotated_any(t) for t in fdef.type.arg_types): | ||
self.fail(messages.ARGUMENT_TYPE_EXPECTED, fdef) | ||
|
@@ -2211,6 +2223,8 @@ def check_return_stmt(self, s: ReturnStmt) -> None: | |
if defn.is_generator: | ||
return_type = self.get_generator_return_type(self.return_types[-1], | ||
defn.is_coroutine) | ||
elif defn.is_coroutine: | ||
return_type = self.get_coroutine_return_type(self.return_types[-1]) | ||
else: | ||
return_type = self.return_types[-1] | ||
|
||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -398,8 +398,7 @@ def do_func_def(self, n: Union[ast3.FunctionDef, ast3.AsyncFunctionDef], | |
self.as_required_block(n.body, n.lineno), | ||
func_type) | ||
if is_coroutine: | ||
# A coroutine is also a generator, mostly for internal reasons. | ||
func_def.is_generator = func_def.is_coroutine = True | ||
func_def.is_coroutine = True | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is good you also cleaned this up. |
||
if func_type is not None: | ||
func_type.definition = func_def | ||
func_type.line = n.lineno | ||
|
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
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
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
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
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.
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.
Is the
is_coroutine
argument here still useful, or can we remove it now?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.
I'm pretty sure we still need it to handle asynchronous generators. In that case, they have both
is_generator=True
andis_coroutine=True
.