Question about maintenance of the Async and Sync client function signatures #2619
-
|
I am working on a library that has support for sync and async and I quite like the way HTTPX does it. A BaseClient class with no IO that regroups everything that can be shared between sync and async Client(BaseClient) the "sync" version of the client The method signature between the sync and async clients are then identical with the exception of they keyword async in front of the AsyncClient version of it. Those functions are not implemented at all in the BaseClient. My main question is, how do you maintain those 2 signatures? Is it just manual or is there a way to test for the difference during CI/CD to make sure that both public methods with the same name have the same parameters/response types def options(
self,
url: URLTypes,
*,
params: typing.Optional[QueryParamTypes] = None,
headers: typing.Optional[HeaderTypes] = None,
cookies: typing.Optional[CookieTypes] = None,
auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,
timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
extensions: typing.Optional[RequestExtensions] = None,
) -> Response:
"""
Send an `OPTIONS` request.
**Parameters**: See `httpx.request`.
"""
return self.request(
"OPTIONS",
url,
params=params,
headers=headers,
cookies=cookies,
auth=auth,
follow_redirects=follow_redirects,
timeout=timeout,
extensions=extensions,
)and async async def options(
self,
url: URLTypes,
*,
params: typing.Optional[QueryParamTypes] = None,
headers: typing.Optional[HeaderTypes] = None,
cookies: typing.Optional[CookieTypes] = None,
auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,
timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
extensions: typing.Optional[RequestExtensions] = None,
) -> Response:
"""
Send an `OPTIONS` request.
**Parameters**: See `httpx.request`.
"""
return await self.request(
"OPTIONS",
url,
params=params,
headers=headers,
cookies=cookies,
auth=auth,
follow_redirects=follow_redirects,
timeout=timeout,
extensions=extensions,
) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Good question. So... There's not actually too much duplicated async/sync code in That applies to the This is different to the approach that we take in the The auto-generation approach is based on ideas from the |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.

Good question. So... There's not actually too much duplicated async/sync code in
httpx. In this case we just manage that maintainance via code review. As you say, "manually".That applies to the
ClientandAsyncClientcases, and also to a few methods on theResponsemodel, such asread()vs.aread().This is different to the approach that we take in the
httpcorepackage, which is the networking layer we use inhttpx. In that case we have a script that gener…