-
Notifications
You must be signed in to change notification settings - Fork 233
service implementation stub generation #170
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 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
c4f1aad
added servicer stub generation
w4rum 694be73
hacky workaround to make test compatible with test framework
w4rum 2030b87
make test compatible with macOS and python 3.6
w4rum 207233b
cut down on duplicate code
w4rum 61e1799
fix: remove name mangling from ServiceImplementation ABC
w4rum a54d0ce
return type hint for wrapper; use method.route
w4rum 54af074
renamed ServiceImplementation; refactored test
w4rum 2a87542
moved ServiceBase import into generated file
w4rum 5bbe19a
renames to improve consistency; inlined unary server response translate
w4rum b73bd0b
fixed test
w4rum 99e6379
removed left-over method; fixed outdated parameter names
w4rum 2ff45e7
fixed outdated parameter name in template
w4rum eac05e9
removed unused / obsolete property function
w4rum 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| from abc import ABC | ||
| from collections import AsyncIterable | ||
| from typing import Callable, Any, Dict | ||
|
|
||
| import grpclib | ||
| import grpclib.server | ||
|
|
||
|
|
||
| class ServiceBase(ABC): | ||
| """ | ||
| Base class for async gRPC servers. | ||
| """ | ||
|
|
||
| async def _call_rpc_handler_server_unary( | ||
| self, | ||
| handler: Callable, | ||
| stream: grpclib.server.Stream, | ||
| request_kwargs: Dict[str, Any], | ||
| ) -> None: | ||
|
|
||
| response = await handler(**request_kwargs) | ||
| await stream.send_message(response) | ||
|
|
||
| async def _call_rpc_handler_server_stream( | ||
| self, | ||
| handler: Callable, | ||
| stream: grpclib.server.Stream, | ||
| request_kwargs: Dict[str, Any], | ||
| ) -> None: | ||
|
|
||
| response_iter = handler(**request_kwargs) | ||
| # check if response is actually an AsyncIterator | ||
| # this might be false if the method just returns without | ||
| # yielding at least once | ||
| # in that case, we just interpret it as an empty iterator | ||
| if isinstance(response_iter, AsyncIterable): | ||
| async for response_message in response_iter: | ||
| await stream.send_message(response_message) | ||
| else: | ||
| response_iter.close() | ||
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 |
|---|---|---|
|
|
@@ -279,6 +279,10 @@ def proto_name(self) -> str: | |
| def py_name(self) -> str: | ||
| return pythonize_class_name(self.proto_name) | ||
|
|
||
| @property | ||
| def py_name_as_field(self) -> str: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. looks like this isn't used either actually?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not anymore since 5bbe19a. Nice catch. Good thing you're looking over this. I completely forgot. |
||
| return pythonize_field_name(self.proto_name) | ||
|
|
||
| @property | ||
| def annotation(self) -> str: | ||
| if self.repeated: | ||
|
|
@@ -553,12 +557,17 @@ class ServiceCompiler(ProtoContentBase): | |
| def __post_init__(self) -> None: | ||
| # Add service to output file | ||
| self.output_file.services.append(self) | ||
| self.output_file.typing_imports.add("Dict") | ||
| super().__post_init__() # check for unset fields | ||
|
|
||
| @property | ||
| def proto_name(self) -> str: | ||
| return self.proto_obj.name | ||
|
|
||
| @property | ||
| def full_proto_name(self) -> str: | ||
| return f"{self.parent.package_proto_obj.package}.{self.proto_obj.name}" | ||
|
w4rum marked this conversation as resolved.
Outdated
|
||
|
|
||
| @property | ||
| def py_name(self) -> str: | ||
| return pythonize_class_name(self.proto_name) | ||
|
|
||
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,23 @@ | ||
| syntax = "proto3"; | ||
|
|
||
| package example_service; | ||
|
|
||
| service ExampleService { | ||
|
nat-n marked this conversation as resolved.
Outdated
|
||
| rpc ExampleUnaryUnary(ExampleRequest) returns (ExampleResponse); | ||
| rpc ExampleUnaryStream(ExampleRequest) returns (stream ExampleResponse); | ||
| rpc ExampleStreamUnary(stream ExampleRequest) returns (ExampleResponse); | ||
| rpc ExampleStreamStream(stream ExampleRequest) returns (stream ExampleResponse); | ||
| } | ||
|
|
||
| message ExampleRequest { | ||
| string example_string = 1; | ||
| int64 example_integer = 2; | ||
| } | ||
|
|
||
| message ExampleResponse { | ||
| string example_string = 1; | ||
| int64 example_integer = 2; | ||
| } | ||
|
|
||
| // Suppress test framework error when it's looking for a "Test" message or service | ||
| message Test {} | ||
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,95 @@ | ||
| from typing import AsyncIterator, AsyncIterable | ||
|
|
||
| import pytest | ||
| from grpclib.testing import ChannelFor | ||
|
|
||
| from tests.output_betterproto.example_service.example_service import ( | ||
| ExampleServiceBase, | ||
| ExampleServiceStub, | ||
| ExampleRequest, | ||
| ExampleResponse, | ||
| ) | ||
|
|
||
|
|
||
| class ExampleService(ExampleServiceBase): | ||
| async def example_unary_unary( | ||
| self, example_string: str, example_integer: int | ||
| ) -> "ExampleResponse": | ||
| return ExampleResponse( | ||
| example_string=example_string, | ||
| example_integer=example_integer, | ||
| ) | ||
|
|
||
| async def example_unary_stream( | ||
| self, example_string: str, example_integer: int | ||
| ) -> AsyncIterator["ExampleResponse"]: | ||
| response = ExampleResponse( | ||
| example_string=example_string, | ||
| example_integer=example_integer, | ||
| ) | ||
| yield response | ||
| yield response | ||
| yield response | ||
|
|
||
| async def example_stream_unary( | ||
| self, example_request_iterator: AsyncIterable["ExampleRequest"] | ||
|
w4rum marked this conversation as resolved.
Outdated
|
||
| ) -> "ExampleResponse": | ||
| async for example_request in example_request_iterator: | ||
| return ExampleResponse( | ||
| example_string=example_request.example_string, | ||
| example_integer=example_request.example_integer, | ||
| ) | ||
|
|
||
| async def example_stream_stream( | ||
| self, example_request_iterator: AsyncIterable["ExampleRequest"] | ||
| ) -> AsyncIterator["ExampleResponse"]: | ||
| async for example_request in example_request_iterator: | ||
| yield ExampleResponse( | ||
| example_string=example_request.example_string, | ||
| example_integer=example_request.example_integer, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_calls_with_different_cardinalities(): | ||
| test_string = "test string" | ||
| test_int = 42 | ||
|
|
||
| async with ChannelFor([ExampleService()]) as channel: | ||
| stub = ExampleServiceStub(channel) | ||
|
|
||
| # unary unary | ||
| response = await stub.example_unary_unary( | ||
| example_string="test string", | ||
| example_integer=42, | ||
| ) | ||
| assert response.example_string == test_string | ||
| assert response.example_integer == test_int | ||
|
|
||
| # unary stream | ||
| async for response in stub.example_unary_stream( | ||
| example_string="test string", | ||
| example_integer=42, | ||
| ): | ||
| assert response.example_string == test_string | ||
| assert response.example_integer == test_int | ||
|
|
||
| # stream unary | ||
| request = ExampleRequest( | ||
| example_string=test_string, | ||
| example_integer=42, | ||
| ) | ||
|
|
||
| async def request_iterator(): | ||
| yield request | ||
| yield request | ||
| yield request | ||
|
|
||
| response = await stub.example_stream_unary(request_iterator()) | ||
| assert response.example_string == test_string | ||
| assert response.example_integer == test_int | ||
|
|
||
| # stream stream | ||
| async for response in stub.example_stream_stream(request_iterator()): | ||
| assert response.example_string == test_string | ||
| assert response.example_integer == test_int | ||
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.