Skip to content
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

Do not wrap AsyncIterator and Coroutine with extra Coroutine in async functions #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 10 additions & 3 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1003,7 +1003,9 @@ def get_coroutine_return_type(self, return_type: Type) -> Type:
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]
if len(return_type.args) >= 3:
return return_type.args[2]
return AnyType(TypeOfAny.from_error)

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)."""
Expand Down Expand Up @@ -1355,8 +1357,13 @@ def check_func_def(
self.binder.pop_frame(True, 0)

if not unreachable:
if defn.is_generator or is_named_instance(
self.return_types[-1], "typing.AwaitableGenerator"
if (
defn.is_generator
or is_named_instance(self.return_types[-1], "typing.AwaitableGenerator")
or (
(t := getattr(self.return_types[-1], "type", None))
and t.has_base("typing.AsyncIterator")
)
):
return_type = self.get_generator_return_type(
self.return_types[-1], defn.is_coroutine
Expand Down
8 changes: 6 additions & 2 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -979,9 +979,13 @@ def analyze_func_def(self, defn: FuncDef) -> None:
and isinstance(defn.type, CallableType)
and self.wrapped_coro_return_types.get(defn) != defn.type
):
if defn.is_async_generator:
# Async generator types are handled elsewhere
ret_typeinfo = getattr(defn.type.ret_type, "type", None)
if ret_typeinfo and ret_typeinfo.has_base("typing.AsyncIterator"):
defn.is_coroutine = True
defn.is_async_generator = True
pass
elif ret_typeinfo and ret_typeinfo.has_base("typing.Awaitable"):
defn.is_coroutine = True
else:
# A coroutine defined as `async def foo(...) -> T: ...`
# has external return type `Coroutine[Any, Any, T]`.
Expand Down
13 changes: 5 additions & 8 deletions test-data/unit/check-async-await.test
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,6 @@ async def f() -> None:
[builtins fixtures/async_await.pyi]
[typing fixtures/typing-async.pyi]
[out]
main:7: error: "Coroutine[Any, Any, AsyncGenerator[str, None]]" has no attribute "__aiter__" (not async iterable)
main:7: note: Maybe you forgot to use "await"?

[case testAsyncForErrorCanBeIgnored]

Expand Down Expand Up @@ -683,14 +681,13 @@ from typing import AsyncGenerator, Any
async def f() -> AsyncGenerator[Any, Any]:
yield None

async def h() -> Any:
async def h() -> Any: # E: The return type of an async generator function should be "AsyncGenerator" or one of its supertypes
yield 0

async def g(): # E: Function is missing a return type annotation
yield 0
[builtins fixtures/async_await.pyi]
[typing fixtures/typing-async.pyi]
[out]

[case testAsyncOverloadedFunction]
from typing import overload
Expand Down Expand Up @@ -915,7 +912,7 @@ async def f() -> AsyncGenerator[int, None]:
async def g(x: int) -> int:
return x

return (await g(x) for x in [1, 2, 3])
return (await g(x) for x in [1, 2, 3]) # E: "return" with value in async generator is not allowed

[builtins fixtures/async_await.pyi]
[typing fixtures/typing-async.pyi]
Expand Down Expand Up @@ -1054,10 +1051,10 @@ class P(Protocol):
raise BaseException

class Launcher(P):
def launch(self) -> AsyncIterator[int]: # E: Return type "AsyncIterator[int]" of "launch" incompatible with return type "Coroutine[Any, Any, AsyncIterator[int]]" in supertype "P" \
# N: Consider declaring "launch" in supertype "P" without "async" \
# N: See https://mypy.readthedocs.io/en/stable/more_types.html#asynchronous-iterators
def launch(self) -> AsyncIterator[int]:
raise BaseException



[builtins fixtures/async_await.pyi]
[typing fixtures/typing-async.pyi]
46 changes: 46 additions & 0 deletions test-data/unit/check-functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -3382,3 +3382,49 @@ for factory in (
reveal_type(factory) # N: Revealed type is "def () -> Union[builtins.str, None]"
var = factory()
[builtins fixtures/tuple.pyi]

[case testAsyncIteratorOverload]
from typing import AsyncIterator, Callable, Iterator, TypeVar, Union, overload

T = TypeVar("T")
U = TypeVar("U")

def deco(fn: Callable[[T], AsyncIterator[U]]) -> Callable[[T], U]:
...

@overload
@deco
async def test_async(val: int) -> AsyncIterator[int]: ...

@overload
@deco
async def test_async(val: str) -> AsyncIterator[str]: ...

@deco
async def test_async(val: Union[int, str]) -> AsyncIterator[Union[int, str]]:
yield val

[typing fixtures/typing-async.pyi]

[case testAsyncIteratorOverloadMixedAsync]
from typing import AsyncIterator, Callable, Iterator, TypeVar, Union, overload

T = TypeVar("T")
U = TypeVar("U")

def deco(fn: Callable[[T], AsyncIterator[U]]) -> Callable[[T], U]:
...

@overload
@deco
def test_async(val: int) -> AsyncIterator[int]: ...

@overload
@deco
def test_async(val: str) -> AsyncIterator[str]: ...

@deco
async def test_async(val: Union[int, str]) -> AsyncIterator[Union[int, str]]:
yield val

[typing fixtures/typing-async.pyi]
Loading