Skip to content

fix: support non-coroutine awaitables in run_in_loop #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 2, 2025

Conversation

JasperSui
Copy link
Owner

Support Non-Coroutine Awaitables in run_in_loop

Problem

The run_in_loop method previously only properly handled coroutines and futures, but not other awaitable objects like those returned by asyncio.gather(). This caused issues when trying to run gathered tasks in a background thread loop.

#100

Solution

  • Changed the parameter type from Coroutine[Any, Any, T] | asyncio.Future[T] to the more general Awaitable[T]
  • Added logic to detect if the awaitable is a coroutine, and if not, wrap it in a coroutine function
  • This allows run_in_loop to properly handle any awaitable object, including those returned by asyncio.gather()

Testing

  • Added specific tests for using run_in_loop with asyncio.gather()
  • Added a test for run_coroutine_sync with asyncio.gather()

Impact

This change maintains backward compatibility while extending functionality to support more awaitable types, making the library more flexible for various async patterns.

- Modified run_in_loop to accept any Awaitable[T] instead of just Coroutine or Future
- Added wrapper function to handle non-coroutine awaitables like asyncio.gather
- Updated type hints and documentation
- Added tests for asyncio.gather support
@JasperSui JasperSui force-pushed the fix/run-in-loop-handle-awaitable-correctly branch from 2ee8c4c to 9b9047a Compare May 2, 2025 15:36
Copy link

codecov bot commented May 2, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (b916643) to head (9b9047a).
Report is 3 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff            @@
##              main      #101   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            9         9           
  Lines          262       265    +3     
  Branches        28        28           
=========================================
+ Hits           262       265    +3     
Files with missing lines Coverage Δ
src/fastapi_injectable/concurrency.py 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@JasperSui JasperSui merged commit e2dae7c into main May 2, 2025
45 checks passed
@JasperSui JasperSui deleted the fix/run-in-loop-handle-awaitable-correctly branch May 2, 2025 15:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant