Skip to content

Commit

Permalink
Add hint for AsyncIterator incompatible return type
Browse files Browse the repository at this point in the history
  • Loading branch information
ikonst committed Aug 16, 2023
1 parent 14418bc commit 4fea3c5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
22 changes: 22 additions & 0 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,14 @@ def signature_incompatible_with_supertype(
code=code,
)

# if (
# isinstance(original, (CallableType, Overloaded)) and
# isinstance(override, (CallableType, Overloaded))
# ):
# print(original)
# print(override)
# breakpoint()

def pretty_callable_or_overload(
self,
tp: CallableType | Overloaded,
Expand Down Expand Up @@ -1310,6 +1318,20 @@ def return_type_incompatible_with_supertype(
code=codes.OVERRIDE,
)

if (
isinstance(original, Instance)
and isinstance(override, Instance)
and override.type.fullname == "typing.AsyncIterator"
and original.type.fullname == "typing.Coroutine"
and len(original.args) == 3
and original.args[2] == override
):
self.note(f'Consider declaring "{name}" in {target} without "async"', context)
self.note(
"See https://mypy.readthedocs.io/en/stable/more_types.html#asynchronous-iterators",
context,
)

def override_target(self, name: str, name_in_super: str, supertype: str) -> str:
target = f'supertype "{supertype}"'
if name_in_super != name:
Expand Down
16 changes: 16 additions & 0 deletions test-data/unit/check-async-await.test
Original file line number Diff line number Diff line change
Expand Up @@ -1021,3 +1021,19 @@ def coro() -> Generator[int, None, None]:
reveal_type(coro) # N: Revealed type is "def () -> typing.AwaitableGenerator[builtins.int, None, None, typing.Generator[builtins.int, None, None]]"
[builtins fixtures/async_await.pyi]
[typing fixtures/typing-async.pyi]

[case asyncIteratorInProtocol]
from typing import AsyncIterator, Protocol

class P(Protocol):
async def launch(self) -> AsyncIterator[int]:
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
raise BaseException

[builtins fixtures/async_await.pyi]
[typing fixtures/typing-async.pyi]

0 comments on commit 4fea3c5

Please sign in to comment.