-
Notifications
You must be signed in to change notification settings - Fork 2
Add support for generating protobuf compliant DESCRIPTORs #112
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
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
597d63e
Add support for generate protobuf compliant DESCRIPTORs
edaniels 44e023e
fix for dependent pkgs
edaniels 5c7548e
fix multiple descriptors
edaniels 2227f36
driveby
edaniels af37fdc
comment
edaniels 91e9be4
order imports
edaniels e53ea36
fix nested descriptors and stop mutating file input
edaniels ce69a38
lint
edaniels 8b4ce01
fix
edaniels a85929c
fix default groups
edaniels 485954e
Revert "fix default groups"
edaniels 5dd0efc
better descriptor
edaniels 2cede42
update readme
edaniels b6aedcb
Merge branch 'main' into descriptors
edaniels aa3af0c
pr changes
edaniels deb851f
lint
edaniels 9da89da
remove import_suffix
edaniels 38c865b
remove source code info from descriptors
edaniels File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Google Protobuf Descriptors | ||
|
||
Google's protoc plugin for Python generated DESCRIPTOR fields that enable reflection capabilities in many libraries (e.g. grpc, grpclib, mcap). | ||
|
||
By default, betterproto2 doesn't generate these as it introduces a dependency on `protobuf`. If you're okay with this dependency and want to generate DESCRIPTORs, use the compiler option `python_betterproto2_opt=google_protobuf_descriptors`. | ||
|
||
|
||
## grpclib Reflection | ||
|
||
In order to properly use reflection right now, you will need to modify the `DescriptorPool` that is used by grpclib's `ServerReflection`. To do so, take a look at the use of `ServerReflection.extend` in the `test_grpclib_reflection` test in https://github.com/vmagamedov/grpclib/blob/master/tests/grpc/test_grpclib_reflection.py | ||
In the future, once https://github.com/vmagamedov/grpclib/pull/204 is merged, you will be able to pass the `default_google_proto_descriptor_pool` into the `ServerReflection.extend` class method. |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import asyncio | ||
from typing import Generic, TypeVar | ||
|
||
import pytest | ||
from google.protobuf import descriptor_pb2 | ||
from grpclib.reflection.service import ServerReflection | ||
from grpclib.reflection.v1.reflection_grpc import ServerReflectionBase as ServerReflectionBaseV1 | ||
from grpclib.reflection.v1alpha.reflection_grpc import ServerReflectionBase as ServerReflectionBaseV1Alpha | ||
from grpclib.testing import ChannelFor | ||
|
||
from tests.output_betterproto.example_service import TestBase | ||
from tests.output_betterproto.grpc.reflection.v1 import ( | ||
ErrorResponse, | ||
ListServiceResponse, | ||
ServerReflectionRequest, | ||
ServerReflectionStub, | ||
ServiceResponse, | ||
) | ||
from tests.output_betterproto_descriptor.google_proto_descriptor_pool import default_google_proto_descriptor_pool | ||
|
||
|
||
class TestService(TestBase): | ||
pass | ||
|
||
|
||
T = TypeVar("T") | ||
|
||
|
||
class AsyncIterableQueue(Generic[T]): | ||
CLOSED_SENTINEL = object() | ||
|
||
def __init__(self): | ||
self._queue = asyncio.Queue() | ||
self._done = asyncio.Event() | ||
|
||
def put(self, item: T): | ||
self._queue.put_nowait(item) | ||
|
||
def close(self): | ||
self._queue.put_nowait(self.CLOSED_SENTINEL) | ||
|
||
def __aiter__(self): | ||
return self | ||
|
||
async def __anext__(self) -> T: | ||
val = await self._queue.get() | ||
if val is self.CLOSED_SENTINEL: | ||
raise StopAsyncIteration | ||
return val | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_grpclib_reflection(): | ||
service = TestService() | ||
services = ServerReflection.extend([service]) | ||
for service in services: | ||
# This won't be needed once https://github.com/vmagamedov/grpclib/pull/204 is in. | ||
if isinstance(service, ServerReflectionBaseV1Alpha | ServerReflectionBaseV1): | ||
service._pool = default_google_proto_descriptor_pool | ||
|
||
async with ChannelFor(services) as channel: | ||
requests = AsyncIterableQueue[ServerReflectionRequest]() | ||
responses = ServerReflectionStub(channel).server_reflection_info(requests) | ||
|
||
# list services | ||
requests.put(ServerReflectionRequest(list_services="")) | ||
response = await anext(responses) | ||
assert response.list_services_response == ListServiceResponse( | ||
service=[ServiceResponse(name="example_service.Test")] | ||
) | ||
|
||
# list methods | ||
|
||
# should fail before we've added descriptors to the protobuf pool | ||
requests.put(ServerReflectionRequest(file_containing_symbol="example_service.Test")) | ||
response = await anext(responses) | ||
assert response.error_response == ErrorResponse(error_code=5, error_message="not found") | ||
assert response.file_descriptor_response is None | ||
|
||
# now it should work | ||
import tests.output_betterproto_descriptor.example_service as example_service_with_desc | ||
|
||
requests.put(ServerReflectionRequest(file_containing_symbol="example_service.Test")) | ||
response = await anext(responses) | ||
expected = descriptor_pb2.FileDescriptorProto.FromString( | ||
example_service_with_desc.EXAMPLE_SERVICE_PROTO_DESCRIPTOR.serialized_pb | ||
) | ||
assert response.error_response is None | ||
assert response.file_descriptor_response is not None | ||
assert len(response.file_descriptor_response.file_descriptor_proto) == 1 | ||
actual = descriptor_pb2.FileDescriptorProto.FromString( | ||
response.file_descriptor_response.file_descriptor_proto[0] | ||
) | ||
assert actual == expected | ||
|
||
requests.close() | ||
|
||
await anext(responses, None) |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import pytest | ||
|
||
from tests.output_betterproto.import_cousin_package_same_name.test.subpackage import Test | ||
|
||
# importing the cousin should cause no descriptor pool errors since the subpackage imports it once already | ||
from tests.output_betterproto_descriptor.import_cousin_package_same_name.cousin.subpackage import CousinMessage | ||
from tests.output_betterproto_descriptor.import_cousin_package_same_name.test.subpackage import Test as TestWithDesc | ||
|
||
|
||
def test_message_enum_descriptors(): | ||
# Normally descriptors are not available as they require protobuf support | ||
# to inteoperate with other libraries. | ||
with pytest.raises(AttributeError): | ||
Test.DESCRIPTOR.full_name | ||
|
||
# But the python_betterproto2_opt=google_protobuf_descriptors option | ||
# will add them in as long as protobuf is depended on. | ||
assert TestWithDesc.DESCRIPTOR.full_name == "import_cousin_package_same_name.test.subpackage.Test" | ||
assert CousinMessage.DESCRIPTOR.full_name == "import_cousin_package_same_name.cousin.subpackage.CousinMessage" |
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.