From 66d38ecaaad65a985936ec55ca73c938ed31977e Mon Sep 17 00:00:00 2001 From: bzoracler <50305397+bzoracler@users.noreply.github.com> Date: Mon, 18 Nov 2024 18:17:28 +1300 Subject: [PATCH 1/2] fix: preserve generator error context --- mypy/checker.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/mypy/checker.py b/mypy/checker.py index 1bee348bc252..7bd454dc80cf 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -1223,11 +1223,14 @@ def check_func_def( if defn.is_async_generator: if not self.is_async_generator_return_type(typ.ret_type): self.fail( - message_registry.INVALID_RETURN_TYPE_FOR_ASYNC_GENERATOR, typ + message_registry.INVALID_RETURN_TYPE_FOR_ASYNC_GENERATOR, + typ.ret_type, ) else: if not self.is_generator_return_type(typ.ret_type, defn.is_coroutine): - self.fail(message_registry.INVALID_RETURN_TYPE_FOR_GENERATOR, typ) + self.fail( + message_registry.INVALID_RETURN_TYPE_FOR_GENERATOR, typ.ret_type + ) # Fix the type if decorated with `@types.coroutine` or `@asyncio.coroutine`. if defn.is_awaitable_coroutine: From 8f982d5ff32e2cdc7d0690b614ad3724231d93c0 Mon Sep 17 00:00:00 2001 From: bzoracler <50305397+bzoracler@users.noreply.github.com> Date: Mon, 18 Nov 2024 18:49:50 +1300 Subject: [PATCH 2/2] fix: try with the definition context --- mypy/checker.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/mypy/checker.py b/mypy/checker.py index 7bd454dc80cf..97799056e62e 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -1223,14 +1223,11 @@ def check_func_def( if defn.is_async_generator: if not self.is_async_generator_return_type(typ.ret_type): self.fail( - message_registry.INVALID_RETURN_TYPE_FOR_ASYNC_GENERATOR, - typ.ret_type, + message_registry.INVALID_RETURN_TYPE_FOR_ASYNC_GENERATOR, defn ) else: if not self.is_generator_return_type(typ.ret_type, defn.is_coroutine): - self.fail( - message_registry.INVALID_RETURN_TYPE_FOR_GENERATOR, typ.ret_type - ) + self.fail(message_registry.INVALID_RETURN_TYPE_FOR_GENERATOR, defn) # Fix the type if decorated with `@types.coroutine` or `@asyncio.coroutine`. if defn.is_awaitable_coroutine: