Make typing.Coroutine a protocol - #16419
Merged
Merged
Conversation
Contributor
|
Diff from mypy_primer, showing the effect of this PR on open source code: prefect (https://github.com/PrefectHQ/prefect)
- src/prefect/utilities/asyncutils/__init__.py:361: error: Incompatible return value type (got "_Wrapped[P, Coroutine[Any, Any, Any], [VarArg(Any), DefaultNamedArg(bool | None, '_sync'), KwArg(Any)], Coroutine[Any, Any, Any]]", expected "Callable[P, R? | Coroutine[Any, Any, R?]]") [return-value]
+ src/prefect/utilities/asyncutils/__init__.py:361: error: Incompatible return value type (got "_Wrapped[P, Coroutine[Any, Any, R?], [VarArg(Any), DefaultNamedArg(bool | None, '_sync'), KwArg(Any)], Coroutine[Any, Any, R?]]", expected "Callable[P, R? | Coroutine[Any, Any, R?]]") [return-value]
- src/prefect/utilities/asyncutils/__init__.py:361: note: "_Wrapped[P, Coroutine[Any, Any, Any], [VarArg(Any), DefaultNamedArg(bool | None, '_sync'), KwArg(Any)], Coroutine[Any, Any, Any]].__call__" has type "def __call__(*args: Any, _sync: bool | None = ..., **kwargs: Any) -> Coroutine[Any, Any, Any]"
+ src/prefect/utilities/asyncutils/__init__.py:361: note: "_Wrapped[P, Coroutine[Any, Any, R?], [VarArg(Any), DefaultNamedArg(bool | None, '_sync'), KwArg(Any)], Coroutine[Any, Any, R?]].__call__" has type "def __call__(*args: Any, _sync: bool | None = ..., **kwargs: Any) -> Coroutine[Any, Any, R?]"
|
AlexWaygood
marked this pull request as ready for review
September 20, 2026 19:28
AlexWaygood
commented
Sep 20, 2026
Comment on lines
-607
to
-608
| __name__: str | ||
| __qualname__: str |
Member
Author
There was a problem hiding this comment.
these aren't required for an isinstance() check to pass at runtime, and I don't think __name__ or __qualname__ attributes are really relevant to the abstract idea of what it means for a nominal class (like types.CoroutineType) to be coroutine-like. So I don't think it's appropriate for them to be listed here.
types.CoroutineType, the concrete, nominal type returned by async def functions at runtime, should have these attributes, of course, and still does on this branch:
Lines 428 to 431 in 57035b1
srittau
approved these changes
Sep 20, 2026
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.
This was in the original diff for #10816 but got removed before the PR was merged -- it looks like the removal might have been an accidental change as part of a merge commit. At runtime
isinstance()checks against this class use duck-typing, and we already havetyping.Generatoras a protocol: