-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Remove generic type from
BaseApiClient
"
This change was intended to make building a client simpler, but it ended up being more complicated than expected, using the async stub is more complicated than expected, as it lives only in the `.pyi` file and can't be used in any other context than type hints. For example the new approach didn't worked well with delaying the connection of the client. To handle that correctly, more work is needed by subclasses. This commit reverts back to making the `BaseApiClient` class generic and it instantiates the stub internally as before. To get proper async type hints, users now only need to write the `stub` property themselves, and use the appropriate async stub type hint there. This reverts commit 035a794. Signed-off-by: Leandro Lucarella <[email protected]>
- Loading branch information
Showing
3 changed files
with
122 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,60 @@ | ||
# Frequenz Client Base Library Release Notes | ||
|
||
## Summary | ||
|
||
<!-- Here goes a general summary of what this release is about --> | ||
|
||
## Upgrading | ||
|
||
<!-- Here goes notes on how to upgrade from previous versions, including deprecations and what they should be replaced with --> | ||
|
||
## New Features | ||
|
||
<!-- Here goes the main new features and examples or instructions on how to use them --> | ||
|
||
## Bug Fixes | ||
|
||
<!-- Here goes notable bug fixes that are worth a special mention or explanation --> | ||
The `BaseApiClient` class is generic again. There was too many issues with the new approach, so it was rolled back. | ||
|
||
- If you are upgrading from v0.7.x, you should be able to roll back your changes with the upgrade and just keep the new `stub` property. | ||
|
||
```python | ||
# Old | ||
from __future__ import annotations | ||
import my_service_pb2_grpc | ||
class MyApiClient(BaseApiClient): | ||
def __init__(self, server_url: str, *, ...) -> None: | ||
super().__init__(server_url, ...) | ||
stub = my_service_pb2_grpc.MyServiceStub(self.channel) | ||
self._stub: my_service_pb2_grpc.MyServiceAsyncStub = stub # type: ignore | ||
... | ||
|
||
@property | ||
def stub(self) -> my_service_pb2_grpc.MyServiceAsyncStub: | ||
if self.channel is None: | ||
raise ClientNotConnected(server_url=self.server_url, operation="stub") | ||
return self._stub | ||
|
||
# New | ||
from __future__ import annotations | ||
import my_service_pb2_grpc | ||
from my_service_pb2_grpc import MyServiceStub | ||
class MyApiClient(BaseApiClient[MyServiceStub]): | ||
def __init__(self, server_url: str, *, ...) -> None: | ||
super().__init__(server_url, MyServiceStub, ...) | ||
... | ||
|
||
@property | ||
def stub(self) -> my_service_pb2_grpc.MyServiceAsyncStub: | ||
"""The gRPC stub for the API.""" | ||
if self.channel is None or self._stub is None: | ||
raise ClientNotConnected(server_url=self.server_url, operation="stub") | ||
# This type: ignore is needed because we need to cast the sync stub to | ||
# the async stub, but we can't use cast because the async stub doesn't | ||
# actually exists to the eyes of the interpreter, it only exists for the | ||
# type-checker, so it can only be used for type hints. | ||
return self._stub # type: ignore | ||
``` | ||
|
||
- If you are upgrading from v0.6.x, you should only need to add the `stub` property to your client class and then use that property instead of `_stub` in your code. | ||
|
||
```python | ||
@property | ||
def stub(self) -> my_service_pb2_grpc.MyServiceAsyncStub: | ||
"""The gRPC stub for the API.""" | ||
if self.channel is None or self._stub is None: | ||
raise ClientNotConnected(server_url=self.server_url, operation="stub") | ||
# This type: ignore is needed because we need to cast the sync stub to | ||
# the async stub, but we can't use cast because the async stub doesn't | ||
# actually exists to the eyes of the interpreter, it only exists for the | ||
# type-checker, so it can only be used for type hints. | ||
return self._stub # type: ignore | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.