Relax future() and future_safe() argument types from Coroutine to Awaitable - #2480
Open
syzayd wants to merge 1 commit into
Open
Relax future() and future_safe() argument types from Coroutine to Awaitable#2480syzayd wants to merge 1 commit into
syzayd wants to merge 1 commit into
Conversation
Both decorators typed their wrapped function argument as returning Coroutine, which rejects any callable typed as returning a plain Awaitable (e.g. an async def that awaits and returns another awaitable, rather than being annotated as returning a Coroutine itself), even though the runtime behavior only relies on awaiting the result. Widen the parameter type to Awaitable and drop the now unused _SecondType TypeVar. Fixes dry-python#2404
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.
I have made things!
Checklist
Related issues
Closes #2404
What changed
future()andfuture_safe()typed their wrapped function argument as returningCoroutine, which is stricter than needed. A callable typed asCallable[..., Awaitable[T]](for example anasync defthat awaits and returns some other awaitable, rather than one whose own return is annotatedCoroutine) failed type checking even though the decorators only everawaitthe result at runtime.This widens the parameter type on both decorators (and the internal
_future_safe_factoryhelper) toAwaitable, and removes the now unused_SecondTypeTypeVar that only existed to parameterizeCoroutine.Test plan
test_future_decorator.ymlandtest_future_safe_decorator.ymlcovering aCallable[[int], Awaitable[int]]argument that is not itself typed asCoroutine.pytest typesafety/test_future(53 passed)pytest tests -k future(133 passed)mypy returns(no issues)ruff check returns/future.py typesafety/test_future(all checks passed)