-
Notifications
You must be signed in to change notification settings - Fork 30
Running Synchronous Code as Async Using Lambdas (Code Smell) #162
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
Comments
Thanks for the suggestion, @BoggerByte! This makes sense, and it seems like I'm not very familiar with |
Thanks for the thoughtful reply! You're right - allowing users to pass in a custom Session is important. The good news is that httpx supports similar flexibility via custom Client/AsyncClient instances, and even more extensibility through custom transports, which can handle advanced use cases like retries, error handling, or test mocking. We could accept a user-provided httpx.AsyncClient, or default to one internally to preserve flexibility while enabling true async I/O without run_async workarounds. Happy to help sketch out a migration plan or open a PR if this direction sounds good. |
How would you envision the API for it? Right now, |
I’d lean toward accepting an optional Internally, if no client is passed in, we can instantiate a default one with sensible settings. As for So maybe something like this in the constructor: def __init__(self, ..., client: Optional[httpx.AsyncClient] = None):
self._client = client or httpx.AsyncClient(...) That keeps the API simple and idiomatic while staying very flexible. Does that sound aligned with what you had in mind? |
Thank you for expanding! I've brought this internally for debate and will circle back to you soon. |
Thanks for the patience, @BoggerByte! We're generally good with migrating to
If all these are met, we see no problems with transitioning to |
Enhancement description
The
TodoistAPIAsync
class currently wraps synchronous API calls usingrun_async(lambda: self._api.method(...))
. This approach does not truly make the operations asynchronous and introduces inefficiencies, such as blocking the event loop.To properly support async functionality, the project should consider using
httpx
, which provides both sync (httpx.Client
) and async (httpx.AsyncClient
) interfaces. This would allow for a cleaner implementation of async API methods without relying on wrapping sync calls insidelambda
functions.The problem it solves
lambda
wrappers makes the codebase cleaner and easier to maintain.httpx
enables real async HTTP requests without requiring additional workarounds likerun_async
.Additional information
httpx
library is well-supported and designed for both sync and async HTTP requests.The text was updated successfully, but these errors were encountered: