8000 Phase 2 of async/await by gvanrossum · Pull Request #1946 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Phase 2 of async/await #1946

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
merged 23 commits into from
Aug 3, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10000
Prev Previous commit
Next Next commit
Complete the compatibility matrix.
Upgrade AwaitableGenerator to four parameters -- the last one
preserves the original type, which we sometimes need.
  • Loading branch information
Guido van Rossum committed Aug 3, 2016
commit 17bed84be703f504b2469bcd278ed33440ec2202
29 changes: 24 additions & 5 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ def is_generator_return_type(self, typ: Type, is_coroutine: bool) -> bool:
except KeyError:
return False
agt = self.named_generic_type('typing.AwaitableGenerator',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be simpler to just look at the fullname instead, like we do elsewhere?

[AnyType(), AnyType(), AnyType()])
[AnyType(), AnyType(), AnyType(), AnyType()])
return is_equivalent(typ, agt)

def get_generator_yield_type(self, return_type: Type, is_coroutine: bool) -> Type:
Expand Down Expand Up @@ -360,7 +360,7 @@ def get_generator_receive_type(self, return_type: Type, is_coroutine: bool) -> T
# Awaitable, AwaitableGenerator: tc is Any.
return AnyType()
elif (return_type.type.fullname() in ('typing.Generator', 'typing.AwaitableGenerator')
and len(return_type.args) == 3):
and len(return_type.args) >= 3):
# Generator: tc is args[1].
return return_type.args[1]
else:
Expand All @@ -385,7 +385,7 @@ def get_generator_return_type(self, return_type: Type, is_coroutine: bool) -> Ty
# Awaitable: tr is args[0].
return return_type.args[0]
elif (return_type.type.fullname() in ('typing.Generator', 'typing.AwaitableGenerator')
and len(return_type.args) == 3):
and len(return_type.args) >= 3):
# AwaitableGenerator, Generator: tr is args[2].
return return_type.args[2]
else:
Expand Down Expand Up @@ -555,7 +555,8 @@ def is_implicit_any(t: Type) -> bool:
ty = self.get_generator_yield_type(t, c)
tc = self.get_generator_receive_type(t, c)
tr = self.get_generator_return_type(t, c)
ret_type = self.named_generic_type('typing.AwaitableGenerator', [ty, tc, tr])
ret_type = self.named_generic_type('typing.AwaitableGenerator',
[ty, tc, tr, t])
typ = typ.copy_modified(ret_type=ret_type)
defn.type = typ

Expand Down Expand Up @@ -1891,6 +1892,11 @@ def visit_call_expr(self, e: CallExpr) -> Type:
return self.expr_checker.visit_call_expr(e)

def visit_yield_from_expr(self, e: YieldFromExpr) -> Type:
# NOTE: Whether `yield from` accepts an `async def` decorated
# with `@types.coroutine` (or `@asyncio.coroutine`) depends on
# whether the generator containing the `yield from` is itself
# thus decorated. But it accepts a generator regardless of
# how it's decorated.
return_type = self.return_types[-1]
subexpr_type = self.accept(e.expr, return_type)
iter_type = None # type: Type
Expand All @@ -1901,6 +1907,8 @@ def visit_yield_from_expr(self, e: YieldFromExpr) -> Type:
iter_type = AnyType()
elif (isinstance(subexpr_type, Instance) and
is_subtype(subexpr_type, self.named_type('typing.Iterable'))):
if self.is_async_def(subexpr_type) and not self.has_coroutine_decorator(return_type):
self.msg.yield_from_invalid_operand_type(subexpr_type, e)
iter_method_type = self.expr_checker.analyze_external_member_access(
'__iter__',
subexpr_type,
Expand All @@ -1911,7 +1919,8 @@ def visit_yield_from_expr(self, e: YieldFromExpr) -> Type:
iter_type, _ = self.expr_checker.check_call(iter_method_type, [], [],
context=generic_generator_type)
else:
self.msg.yield_from_invalid_operand_type(subexpr_type, e)
if not (self.is_async_def(subexpr_type) and self.has_coroutine_decorator(return_type)):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is iter_type Any in yield from some_async_def?

self.msg.yield_from_invalid_operand_type(subexpr_type, e)
iter_type = AnyType()

# Check that the iterator's item type matches the type yielded by the Generator function
Expand All @@ -1938,6 +1947,16 @@ def visit_yield_from_expr(self, e: YieldFromExpr) -> Type:
else:
return Void()

def has_coroutine_decorator(self, t: Type) -> bool:
"""Whether t came from a function decorated with `@coroutine`."""
return isinstance(t, Instance) and t.type.fullname() == 'typing.AwaitableGenerator'

def is_async_def(self, t: Type) -> bool:
"""Whether t came from a function defined using `async def`."""
if self.has_coroutine_decorator(t) and len(t.args) >= 4:
t = t.args[3]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment about what's going on here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return isinstance(t, Instance) and t.type.fullname() == 'typing.Awaitable'

def visit_member_expr(self, e: MemberExpr) -> Type:
return self.expr_checker.visit_member_expr(e)

Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-async-await.test
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ def plain_host_generator() -> Generator[str, None, None]:
x = yield from plain_generator()
x = yield from plain_coroutine() # E: "yield from" can't be applied to Awaitable[int]
x = yield from decorated_generator()
x = yield from decorated_coroutine() # E: (should error)
x = yield from decorated_coroutine() # E: "yield from" can't be applied to AwaitableGenerator[...]
x = yield from other_iterator()
x = yield from other_coroutine() # E: "yield from" can't be applied to "Aw"

Expand Down
3 changes: 2 additions & 1 deletion test-data/unit/lib-stub/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
T = TypeVar('T')
U = TypeVar('U')
V = TypeVar('V')
S = TypeVar('S')

class Container(Generic[T]):
@abstractmethod
Expand Down Expand Up @@ -61,7 +62,7 @@ class Awaitable(Generic[T]):
@abstractmethod
def __await__(self) -> Generator[Any, Any, T]: pass

class AwaitableGenerator(Generator[T, U, V], Awaitable[V], Generic[T, U, V]):
class AwaitableGenerator(Generator[T, U, V], Awaitable[V], Generic[T, U, V, S]):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is there an AwaitableGenerator in the test data typing stub when there isn't one in the real typing module?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this stub (despite its extension) is actually a stub. It matches the real typing.pyi, not any real typing.py.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh okay. That makes sense.

pass

class AsyncIterable(Generic[T]):
Expand Down
0