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

Barebones grpc aio attempt #276

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion mypy_protobuf/extensions_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions mypy_protobuf/extensions_pb2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
@generated by mypy-protobuf. Do not edit manually!
isort:skip_file
"""
import builtins
import google.protobuf.descriptor
import google.protobuf.descriptor_pb2
import google.protobuf.internal.extension_dict
Expand All @@ -17,3 +18,6 @@ keytype: google.protobuf.internal.extension_dict._ExtensionFieldDescriptor[googl

# Tells mypy to use a specific type for values; only makes sense on map fields
valuetype: google.protobuf.internal.extension_dict._ExtensionFieldDescriptor[google.protobuf.descriptor_pb2.FieldOptions, typing.Text] = ...

# Tells mypy to generate async methods for this service
async_service: google.protobuf.internal.extension_dict._ExtensionFieldDescriptor[google.protobuf.descriptor_pb2.ServiceOptions, builtins.bool] = ...
19 changes: 15 additions & 4 deletions mypy_protobuf/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,9 +681,14 @@ def _input_type(
return result

def _output_type(
self, method: d.MethodDescriptorProto, use_stream_iterator: bool = True
self,
method: d.MethodDescriptorProto,
async_service: bool,
use_stream_iterator: bool = True,
) -> str:
result = self._import_message(method.output_type)
if async_service:
result = "{}[{}]".format(self._import("typing", "Awaitable"), result)
if use_stream_iterator and method.server_streaming:
result = "{}[{}]".format(self._import("typing", "Iterator"), result)
return result
Expand All @@ -700,6 +705,7 @@ def write_grpc_methods(
if not methods:
l("pass")
l("")
async_service = service.options.Extensions[extensions_pb2.async_service]
for i, method in methods:
scl = scl_prefix + [d.ServiceDescriptorProto.METHOD_FIELD_NUMBER, i]
self._write_comments(scl)
Expand All @@ -709,7 +715,7 @@ def write_grpc_methods(
with self._indent():
l("request: {},", self._input_type(method))
l("context: {},", self._import("grpc", "ServicerContext"))
l(") -> {}: ...", self._output_type(method))
l(") -> {}: ...", self._output_type(method, async_service))
l("")

def write_grpc_stub_methods(
Expand All @@ -724,14 +730,15 @@ def write_grpc_stub_methods(
if not methods:
l("pass")
l("")
async_service = service.options.Extensions[extensions_pb2.async_service]
for i, method in methods:
scl = scl_prefix + [d.ServiceDescriptorProto.METHOD_FIELD_NUMBER, i]
self._write_comments(scl)

l("{}:{}[", method.name, self._callable_type(method))
with self._indent():
l("{},", self._input_type(method, False))
l("{}] = ...", self._output_type(method, False))
l("{}] = ...", self._output_type(method, async_service, False))
l("")

def write_grpc_services(
Expand All @@ -749,6 +756,8 @@ def write_grpc_services(
if service.name in PYTHON_RESERVED:
continue

async_service = service.options.Extensions[extensions_pb2.async_service]

scl = scl_prefix + [i]
self._write_comments(scl)

Expand Down Expand Up @@ -776,7 +785,9 @@ def write_grpc_services(
"def add_{}Servicer_to_server(servicer: {}Servicer, server: {}) -> None: ...",
service.name,
service.name,
self._import("grpc", "Server"),
self._import("grpc", "Server")
if async_service
else self._import("grpc", "Server"),
)
l("")

Expand Down
5 changes: 5 additions & 0 deletions proto/mypy_protobuf/extensions.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ extend google.protobuf.FieldOptions {
// Tells mypy to use a specific type for values; only makes sense on map fields
optional string valuetype = 60003;
}

extend google.protobuf.ServiceOptions {
// Tells mypy to generate async methods for this service
optional bool async_service = 60004;
}
9 changes: 8 additions & 1 deletion proto/testproto/grpc/dummy.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ syntax = "proto3";

package dummy;

import "mypy_protobuf/extensions.proto";

message DummyRequest {
string value = 1;
}
Expand All @@ -16,4 +18,9 @@ service DummyService {
rpc UnaryStream (DummyRequest) returns (stream DummyReply) {}
rpc StreamUnary (stream DummyRequest) returns (DummyReply) {}
rpc StreamStream (stream DummyRequest) returns (stream DummyReply) {}
}
}

service DummyAsyncService {
option (mypy_protobuf.async_service) = true;
rpc UnaryUnary (DummyRequest) returns (DummyReply) {}
}
10 changes: 5 additions & 5 deletions run_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ MYPY_PROTOBUF_VENV=venv_$PY_VER_MYPY_PROTOBUF
test "$(protoc-gen-mypy_grpc -V)" = "mypy-protobuf 2.9"
test "$(protoc-gen-mypy_grpc --version)" = "mypy-protobuf 2.9"

# Compile protoc -> mypy using mypy_protobuf
# Prereq - create the mypy.proto python proto
$PROTOC $PROTOC_ARGS --python_out=. `find proto/mypy_protobuf -name "*.proto"`
$PROTOC $PROTOC_ARGS --mypy_out=. `find proto/mypy_protobuf -name "*.proto"`

# Run mypy on mypy-protobuf internal code for developers to catch issues
FILES="mypy_protobuf/main.py"
$MYPY_VENV/bin/mypy --custom-typeshed-dir=$CUSTOM_TYPESHED_DIR --python-executable=$MYPY_PROTOBUF_VENV/bin/python3 --python-version=$PY_VER_MYPY_PROTOBUF_SHORT $FILES
Expand All @@ -96,11 +101,6 @@ MYPY_PROTOBUF_VENV=venv_$PY_VER_MYPY_PROTOBUF
# Compile protoc -> python
$PROTOC $PROTOC_ARGS --python_out=test/generated `find proto -name "*.proto"`

# Compile protoc -> mypy using mypy_protobuf
# Prereq - create the mypy.proto python proto
$PROTOC $PROTOC_ARGS --python_out=. `find proto/mypy_protobuf -name "*.proto"`
$PROTOC $PROTOC_ARGS --mypy_out=. `find proto/mypy_protobuf -name "*.proto"`

# Sanity check that our flags work
$PROTOC $PROTOC_ARGS --mypy_out=quiet:test/generated `find proto -name "*.proto"`
$PROTOC $PROTOC_ARGS --mypy_out=readable_stubs:test/generated `find proto -name "*.proto"`
Expand Down
4 changes: 4 additions & 0 deletions test/generated/mypy_protobuf/extensions_pb2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
@generated by mypy-protobuf. Do not edit manually!
isort:skip_file
"""
import builtins
import google.protobuf.descriptor
import google.protobuf.descriptor_pb2
import google.protobuf.internal.extension_dict
Expand All @@ -17,3 +18,6 @@ keytype: google.protobuf.internal.extension_dict._ExtensionFieldDescriptor[googl

# Tells mypy to use a specific type for values; only makes sense on map fields
valuetype: google.protobuf.internal.extension_dict._ExtensionFieldDescriptor[google.protobuf.descriptor_pb2.FieldOptions, typing.Text] = ...

# Tells mypy to generate async methods for this service
async_service: google.protobuf.internal.extension_dict._ExtensionFieldDescriptor[google.protobuf.descriptor_pb2.ServiceOptions, builtins.bool] = ...
17 changes: 17 additions & 0 deletions test/generated/testproto/grpc/dummy_pb2_grpc.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,20 @@ class DummyServiceServicer(metaclass=abc.ABCMeta):


def add_DummyServiceServicer_to_server(servicer: DummyServiceServicer, server: grpc.Server) -> None: ...

class DummyAsyncServiceStub:
def __init__(self, channel: grpc.Channel) -> None: ...
UnaryUnary:grpc.UnaryUnaryMultiCallable[
global___DummyRequest,
typing.Awaitable[global___DummyReply]] = ...


class DummyAsyncServiceServicer(metaclass=abc.ABCMeta):
@abc.abstractmethod
def UnaryUnary(self,
request: global___DummyRequest,
context: grpc.ServicerContext,
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose we'd want two separate ServicerContext here - depending on whether it's a synchronous service or not - but that's out of my realm of expertise.

) -> typing.Awaitable[global___DummyReply]: ...


def add_DummyAsyncServiceServicer_to_server(servicer: DummyAsyncServiceServicer, server: grpc.Server) -> None: ...
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be server: grpc.aio.Server - but held off on that change until some minimal stub gets into grpc-stubs