From cb90fcb75dc09392132458519b89f06c1ded6b91 Mon Sep 17 00:00:00 2001 From: Nipunn Koorapati Date: Mon, 17 Jan 2022 19:44:03 -0800 Subject: [PATCH] More accurately represent `request` parameter in grpc functions (#336) --- CHANGELOG.md | 3 ++- mypy_protobuf/main.py | 6 +++++- stubtest_allowlist.txt | 3 ++- test/generated/testproto/grpc/dummy_pb2_grpc.pyi | 4 ++-- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 00af5d0d..699af639 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,8 @@ - Support `*_FIELD_NUMBER` for extensions - Bump types-protobuf dependency to 3.19 - Support DESCRIPTOR: ServiceDescriptor in py generic services -- More accurately represent method names in py generic services (done -> callback, self -> inst) +- More accurately represent method names in py generic services (done -> callback, self -> inst) +- More accurately represent method names in grpc services (`request` -> `request_iterator`) - Internal: Get tests to pass on pure-python protobuf impl (minor semantic differences) - Internal: Bump pyright in testing to 1.1.206 - Internal: Use stubtest to validate generated stubs match generated runtime diff --git a/mypy_protobuf/main.py b/mypy_protobuf/main.py index 8b4710ed..82db2739 100644 --- a/mypy_protobuf/main.py +++ b/mypy_protobuf/main.py @@ -750,7 +750,11 @@ def write_grpc_methods( l("@{}", self._import("abc", "abstractmethod")) l("def {}(self,", method.name) with self._indent(): - l("request: {},", self._input_type(method)) + input_name = ( + "request_iterator" if method.client_streaming else "request" + ) + input_type = self._input_type(method) + l(f"{input_name}: {input_type},") l("context: {},", self._import("grpc", "ServicerContext")) l( ") -> {}:{}", diff --git a/stubtest_allowlist.txt b/stubtest_allowlist.txt index 3c524a85..7903de14 100644 --- a/stubtest_allowlist.txt +++ b/stubtest_allowlist.txt @@ -50,6 +50,7 @@ testproto.nested.nested_pb2.AnotherNested.NestedMessage._?NestedEnum2(EnumTypeWr # Some issues with our service stubs for now testproto.test_pb2.ATestService.* -testproto.grpc.dummy_pb2_grpc.DummyServiceServicer.* + +# Part of an "EXPERIMENTAL API" according to comment. Not documented. testproto.grpc.dummy_pb2_grpc.DummyService testproto.grpc.import_pb2_grpc.SimpleService diff --git a/test/generated/testproto/grpc/dummy_pb2_grpc.pyi b/test/generated/testproto/grpc/dummy_pb2_grpc.pyi index fe4352e8..c5345f4a 100644 --- a/test/generated/testproto/grpc/dummy_pb2_grpc.pyi +++ b/test/generated/testproto/grpc/dummy_pb2_grpc.pyi @@ -51,7 +51,7 @@ class DummyServiceServicer(metaclass=abc.ABCMeta): @abc.abstractmethod def StreamUnary(self, - request: typing.Iterator[testproto.grpc.dummy_pb2.DummyRequest], + request_iterator: typing.Iterator[testproto.grpc.dummy_pb2.DummyRequest], context: grpc.ServicerContext, ) -> testproto.grpc.dummy_pb2.DummyReply: """StreamUnary""" @@ -59,7 +59,7 @@ class DummyServiceServicer(metaclass=abc.ABCMeta): @abc.abstractmethod def StreamStream(self, - request: typing.Iterator[testproto.grpc.dummy_pb2.DummyRequest], + request_iterator: typing.Iterator[testproto.grpc.dummy_pb2.DummyRequest], context: grpc.ServicerContext, ) -> typing.Iterator[testproto.grpc.dummy_pb2.DummyReply]: """StreamStream"""