Fix crash on @types.coroutine with unannotated generator function#21436
Closed
Hrk84ya wants to merge 1 commit intopython:masterfrom
Closed
Fix crash on @types.coroutine with unannotated generator function#21436Hrk84ya wants to merge 1 commit intopython:masterfrom
Hrk84ya wants to merge 1 commit intopython:masterfrom
Conversation
…pping - Combine type guard with isinstance check to avoid assertion on potentially None type - Add deferred AwaitableGenerator wrapping for unannotated functions after type inference - Apply generator type transformations (yield, receive, return) after check_func_item infers the function type - Ensures coroutines decorated with @types.coroutine or @asyncio.coroutine are properly wrapped even when type annotations are missing
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Fixes a mypy 2.0 crash when type-checking unannotated generator functions decorated with @types.coroutine / @asyncio.coroutine by deferring AwaitableGenerator wrapping until after type inference when needed.
Changes:
- Replaces an
assertondefn.typewith anisinstance(..., CallableType)guard to avoid crashing when the type is not yet inferred. - Adds a second, idempotent
AwaitableGeneratorwrapping pass aftercheck_func_itemso unannotated coroutines get wrapped post-inference. - Keeps generator yield/receive/return transformations aligned with the inferred function type.
Comments suppressed due to low confidence (1)
mypy/checker.py:5737
- The
AwaitableGeneratorwrapping logic is now duplicated in two places. This increases the risk of future drift (e.g., one block updated but not the other) and makes the control flow harder to reason about. Consider extracting the wrapping into a small helper (e.g.,maybe_wrap_awaitable_generator(defn)) and calling it before and aftercheck_func_item, keeping theCallableTypeguard andis_named_instance(...AwaitableGenerator)idempotency check inside the helper.
if defn.is_awaitable_coroutine and isinstance(defn.type, CallableType):
# Update the return type to AwaitableGenerator (unless we already did).
# Note, this doesn't exist in typing.py, only in typing.pyi.
if not is_named_instance(defn.type.ret_type, "typing.AwaitableGenerator"):
t = defn.type.ret_type
c = defn.is_coroutine
ty = self.get_generator_yield_type(t, c)
tc = self.get_generator_receive_type(t, c)
if c:
tr = self.get_coroutine_return_type(t)
else:
tr = self.get_generator_return_type(t, c)
ret_type = self.named_generic_type("typing.AwaitableGenerator", [ty, tc, tr, t])
typ = defn.type.copy_modified(ret_type=ret_type)
defn.type = typ
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+5747
to
+5761
| # For unannotated functions, defn.type is only available after check_func_item | ||
| # infers it. Apply the AwaitableGenerator wrapping now if it wasn't done above. | ||
| if defn.is_awaitable_coroutine and isinstance(defn.type, CallableType): | ||
| if not is_named_instance(defn.type.ret_type, "typing.AwaitableGenerator"): | ||
| t = defn.type.ret_type | ||
| c = defn.is_coroutine | ||
| ty = self.get_generator_yield_type(t, c) | ||
| tc = self.get_generator_receive_type(t, c) | ||
| if c: | ||
| tr = self.get_coroutine_return_type(t) | ||
| else: | ||
| tr = self.get_generator_return_type(t, c) | ||
| ret_type = self.named_generic_type("typing.AwaitableGenerator", [ty, tc, tr, t]) | ||
| typ = defn.type.copy_modified(ret_type=ret_type) | ||
| defn.type = typ |
Comment on lines
+5747
to
+5761
| # For unannotated functions, defn.type is only available after check_func_item | ||
| # infers it. Apply the AwaitableGenerator wrapping now if it wasn't done above. | ||
| if defn.is_awaitable_coroutine and isinstance(defn.type, CallableType): | ||
| if not is_named_instance(defn.type.ret_type, "typing.AwaitableGenerator"): | ||
| t = defn.type.ret_type | ||
| c = defn.is_coroutine | ||
| ty = self.get_generator_yield_type(t, c) | ||
| tc = self.get_generator_receive_type(t, c) | ||
| if c: | ||
| tr = self.get_coroutine_return_type(t) | ||
| else: | ||
| tr = self.get_generator_return_type(t, c) | ||
| ret_type = self.named_generic_type("typing.AwaitableGenerator", [ty, tc, tr, t]) | ||
| typ = defn.type.copy_modified(ret_type=ret_type) | ||
| defn.type = typ |
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #21426.
Problem
mypy 2.0 crashes with
AssertionErrorinvisit_decorator_innerwhentype-checking an unannotated generator function decorated with
@types.coroutine(or@asyncio.coroutine):