Skip to content

Commit 4fea3c5

Browse files
committed
Add hint for AsyncIterator incompatible return type
1 parent 14418bc commit 4fea3c5

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

mypy/messages.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,6 +1200,14 @@ def signature_incompatible_with_supertype(
12001200
code=code,
12011201
)
12021202

1203+
# if (
1204+
# isinstance(original, (CallableType, Overloaded)) and
1205+
# isinstance(override, (CallableType, Overloaded))
1206+
# ):
1207+
# print(original)
1208+
# print(override)
1209+
# breakpoint()
1210+
12031211
def pretty_callable_or_overload(
12041212
self,
12051213
tp: CallableType | Overloaded,
@@ -1310,6 +1318,20 @@ def return_type_incompatible_with_supertype(
13101318
code=codes.OVERRIDE,
13111319
)
13121320

1321+
if (
1322+
isinstance(original, Instance)
1323+
and isinstance(override, Instance)
1324+
and override.type.fullname == "typing.AsyncIterator"
1325+
and original.type.fullname == "typing.Coroutine"
1326+
and len(original.args) == 3
1327+
and original.args[2] == override
1328+
):
1329+
self.note(f'Consider declaring "{name}" in {target} without "async"', context)
1330+
self.note(
1331+
"See https://mypy.readthedocs.io/en/stable/more_types.html#asynchronous-iterators",
1332+
context,
1333+
)
1334+
13131335
def override_target(self, name: str, name_in_super: str, supertype: str) -> str:
13141336
target = f'supertype "{supertype}"'
13151337
if name_in_super != name:

test-data/unit/check-async-await.test

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,3 +1021,19 @@ def coro() -> Generator[int, None, None]:
10211021
reveal_type(coro) # N: Revealed type is "def () -> typing.AwaitableGenerator[builtins.int, None, None, typing.Generator[builtins.int, None, None]]"
10221022
[builtins fixtures/async_await.pyi]
10231023
[typing fixtures/typing-async.pyi]
1024+
1025+
[case asyncIteratorInProtocol]
1026+
from typing import AsyncIterator, Protocol
1027+
1028+
class P(Protocol):
1029+
async def launch(self) -> AsyncIterator[int]:
1030+
raise BaseException
1031+
1032+
class Launcher(P):
1033+
def launch(self) -> AsyncIterator[int]: # E: Return type "AsyncIterator[int]" of "launch" incompatible with return type "Coroutine[Any, Any, AsyncIterator[int]]" in supertype "P" \
1034+
# N: Consider declaring "launch" in supertype "P" without "async" \
1035+
# N: See https://mypy.readthedocs.io/en/stable/more_types.html#asynchronous-iterators
1036+
raise BaseException
1037+
1038+
[builtins fixtures/async_await.pyi]
1039+
[typing fixtures/typing-async.pyi]

0 commit comments

Comments
 (0)