Skip to content

Commit a7e0f6f

Browse files
authored
Add hint for AsyncIterator incompatible return type (#15883)
For issue described in #5070 and documented in #14973, add a contextual link to the docs.
1 parent 0ae0c75 commit a7e0f6f

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

mypy/messages.py

+16
Original file line numberDiff line numberDiff line change
@@ -1310,6 +1310,22 @@ def return_type_incompatible_with_supertype(
13101310
code=codes.OVERRIDE,
13111311
)
13121312

1313+
original = get_proper_type(original)
1314+
override = get_proper_type(override)
1315+
if (
1316+
isinstance(original, Instance)
1317+
and isinstance(override, Instance)
1318+
and override.type.fullname == "typing.AsyncIterator"
1319+
and original.type.fullname == "typing.Coroutine"
1320+
and len(original.args) == 3
1321+
and original.args[2] == override
1322+
):
1323+
self.note(f'Consider declaring "{name}" in {target} without "async"', context)
1324+
self.note(
1325+
"See https://mypy.readthedocs.io/en/stable/more_types.html#asynchronous-iterators",
1326+
context,
1327+
)
1328+
13131329
def override_target(self, name: str, name_in_super: str, supertype: str) -> str:
13141330
target = f'supertype "{supertype}"'
13151331
if name_in_super != name:

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

+16
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)