Skip to content

Commit 6a6d79e

Browse files
committed
fix return types of athrow and aclose, error when async generator returns a value
1 parent 9eadb4f commit 6a6d79e

File tree

3 files changed

+5
-5
lines changed

3 files changed

+5
-5
lines changed

mypy/checker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1628,7 +1628,7 @@ def check_return_stmt(self, s: ReturnStmt) -> None:
16281628
if isinstance(typ, AnyType):
16291629
return
16301630

1631-
if self.is_unusable_type(return_type):
1631+
if self.is_unusable_type(return_type) or defn.is_async_generator:
16321632
# Lambdas are allowed to have a unusable returns.
16331633
# Functions returning a value of type None are allowed to have a Void return.
16341634
if isinstance(self.scope.top_function(), FuncExpr) or isinstance(typ, NoneTyp):

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ async def h() -> None:
379379
g = gen()
380380
v = await g.asend(1)
381381
reveal_type(v) # E: Revealed type is 'builtins.str*'
382-
await g.athrow(BaseException)
382+
reveal_type(await g.athrow(BaseException)) # E: Revealed type is 'builtins.str*'
383383

384384
[builtins fixtures/dict.pyi]
385385

@@ -419,7 +419,7 @@ from mypy_extensions import AsyncGenerator
419419

420420
async def gen() -> AsyncGenerator[int, None]:
421421
yield 1
422-
return 42
422+
return 42 # E: No return value expected
423423

424424
[builtins fixtures/dict.pyi]
425425

test-data/unit/lib-stub/typing.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ class AsyncGenerator(AsyncIterator[T], Generic[T, U]):
6666
def asend(self, value: U) -> Awaitable[T]: pass
6767

6868
@abstractmethod
69-
def athrow(self, typ: Any, val: Any=None, tb: Any=None) -> Awaitable[None]: pass
69+
def athrow(self, typ: Any, val: Any=None, tb: Any=None) -> Awaitable[T]: pass
7070

7171
@abstractmethod
72-
def aclose(self) -> Awaitable[None]: pass
72+
def aclose(self) -> Awaitable[T]: pass
7373

7474
@abstractmethod
7575
def __aiter__(self) -> 'AsyncGenerator[T, U]': pass

0 commit comments

Comments
 (0)