Skip to content
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

async create client function should not be marked as async #798

Closed
Dartt0n opened this issue May 14, 2024 · 3 comments · Fixed by #805
Closed

async create client function should not be marked as async #798

Dartt0n opened this issue May 14, 2024 · 3 comments · Fixed by #805
Labels
enhancement New feature or request

Comments

@Dartt0n
Copy link
Contributor

Dartt0n commented May 14, 2024

Feature request

Make functions _async.client.create_client and _async.client.AsyncClient.create synchronous.

Problem description

Right now, acreate_client is a re-import of the _async.client.create_client function. This function has been marked as async (see source code). This function, in its body, calls the AsyncClient.create asynchronous class method (see source code). This method itself calls AsyncClient.__init__, which, however, is not asynchronous (see source code). This approach has a negative impact on the user experience for several reasons.
First, this chain of function calls, passing the same arguments with the same names three times until it reaches the destination constructor, seems to introduce a lot of unnecessary abstractions.
The second reason is that most Python applications would instantiate Supabase clients either in synchronous mode (for example, during the creation of an ASGI application in the main loop) or asynchronous mode (when creating multiple clients in asynchronous functions). Both of these approaches can use the synchronous create_client function. However, the first approach cannot use the asynchronous create_client, as this code would be executed in the main loop, not in an asynchronous environment, and therefore a single asynchronous client could not be created in main loop and shared among asynchronous workers.

Proposed Solution

I propose remove async mark from both create_client and AsyncClient.create functions, make create_client call AsyncClient.__init__ directly, and probably, rename re-import from ._async.client import create_client as acreate_client to create_aclient since this naming better represent what function actually does. (not asynchronously create client but create asynchronous client)

- async def create_client(
+ def create_client(
    supabase_url: str,
    supabase_key: str,
    options: Union[ClientOptions, None] = None,
) -> AsyncClient:
-    return await AsyncClient.create(
+    return await AsyncClient(
        supabase_url=supabase_url, supabase_key=supabase_key, options=options
    )
    @classmethod
-    async def create(
+    def create(
        cls,
        supabase_url: str,
        supabase_key: str,
        options: Union[ClientOptions, None] = None,
    ): 
- from ._async.client import create_client as acreate_client
+ from ._async.client import create_client as create_aclient

I believe this solution would be beneficial because acreate_client could be used in both contexts:

  1. when creating a client synchronously for later asynchronous use (e.g., ASGI applications)
  2. when creating the client asynchronously for asynchronous use.

If you have any other opinions or know of any nuances that I have not taken into account, please share them in the discussion. Thank you!

@Dartt0n Dartt0n added the enhancement New feature or request label May 14, 2024
@silentworks
Copy link
Contributor

silentworks commented May 21, 2024

I agree with you about return await AsyncClient.create(, this is remnant of old code that was not removed at the time of some changes that were made to the library. Changing the name now from acreate_client to create_aclient would be a breaking change and doesn't have much benefit to it other than personal naming convention. You can open a PR with the change to

return await AsyncClient(

instead of

return await AsyncClient.create(

@Dartt0n
Copy link
Contributor Author

Dartt0n commented May 22, 2024

Thank you for your feedback. I assume that acreate_client should remain async due to the same compatibility reasons. I have prepared a PR here: #805.

@silentworks
Copy link
Contributor

@Dartt0n thanks for the PR. I have merged it and it should be out in the next release shortly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants