Skip to content

Relax future() and future_safe() argument types from Coroutine to Awaitable - #2480

Open
syzayd wants to merge 1 commit into
dry-python:masterfrom
syzayd:issue-2404
Open

Relax future() and future_safe() argument types from Coroutine to Awaitable#2480
syzayd wants to merge 1 commit into
dry-python:masterfrom
syzayd:issue-2404

Conversation

@syzayd

@syzayd syzayd commented Aug 4, 2026

Copy link
Copy Markdown

I have made things!

Checklist

  • I have double checked that there are no unrelated changes in this pull request (old patches, accidental config files, etc)
  • I have created at least one test case for the changes I have made
  • I have updated the documentation for the changes I have made
  • I have added my changes to the CHANGELOG.md

Related issues

Closes #2404

What changed

future() and future_safe() typed their wrapped function argument as returning Coroutine, which is stricter than needed. A callable typed as Callable[..., Awaitable[T]] (for example an async def that awaits and returns some other awaitable, rather than one whose own return is annotated Coroutine) failed type checking even though the decorators only ever await the result at runtime.

This widens the parameter type on both decorators (and the internal _future_safe_factory helper) to Awaitable, and removes the now unused _SecondType TypeVar that only existed to parameterize Coroutine.

Test plan

  • Added a typesafety case to test_future_decorator.yml and test_future_safe_decorator.yml covering a Callable[[int], Awaitable[int]] argument that is not itself typed as Coroutine.
  • 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)

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

future() and future_safe() overly restrictive type signatures

1 participant