Skip to content

Commit 706bd5a

Browse files
committed
Slightly simplify gRPC helper functions
1 parent 52beeb0 commit 706bd5a

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

betterproto/__init__.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
TypeVar,
2222
Union,
2323
get_type_hints,
24+
TYPE_CHECKING,
2425
)
2526

2627
import grpclib.client
@@ -29,6 +30,9 @@
2930

3031
from .casing import safe_snake_case
3132

33+
if TYPE_CHECKING:
34+
from grpclib._protocols import IProtoMessage
35+
3236
# Proto 3 data types
3337
TYPE_ENUM = "enum"
3438
TYPE_BOOL = "bool"
@@ -420,6 +424,7 @@ class Message(ABC):
420424
register the message fields which get used by the serializers and parsers
421425
to go between Python, binary and JSON protobuf message representations.
422426
"""
427+
423428
_serialized_on_wire: bool
424429
_unknown_fields: bytes
425430
_group_map: Dict[str, dict]
@@ -705,7 +710,7 @@ def to_dict(self, casing: Casing = Casing.CAMEL) -> dict:
705710
for field in dataclasses.fields(self):
706711
meta = FieldMetadata.get(field)
707712
v = getattr(self, field.name)
708-
cased_name = casing(field.name).rstrip("_") # type: ignore
713+
cased_name = casing(field.name).rstrip("_") # type: ignore
709714
if meta.proto_type == "message":
710715
if isinstance(v, datetime):
711716
if v != DATETIME_ZERO:
@@ -741,7 +746,7 @@ def to_dict(self, casing: Casing = Casing.CAMEL) -> dict:
741746
else:
742747
output[cased_name] = b64encode(v).decode("utf8")
743748
elif meta.proto_type == TYPE_ENUM:
744-
enum_values = list(self._cls_for(field)) # type: ignore
749+
enum_values = list(self._cls_for(field)) # type: ignore
745750
if isinstance(v, list):
746751
output[cased_name] = [enum_values[e].name for e in v]
747752
else:
@@ -902,6 +907,7 @@ class _WrappedMessage(Message):
902907
Google protobuf wrapper types base class. JSON representation is just the
903908
value itself.
904909
"""
910+
905911
value: Any
906912

907913
def to_dict(self, casing: Casing = Casing.CAMEL) -> Any:
@@ -982,23 +988,23 @@ def __init__(self, channel: grpclib.client.Channel) -> None:
982988
self.channel = channel
983989

984990
async def _unary_unary(
985-
self, route: str, request_type: Type, response_type: Type[T], request: Any
991+
self, route: str, request: "IProtoMessage", response_type: Type[T]
986992
) -> T:
987993
"""Make a unary request and return the response."""
988994
async with self.channel.request(
989-
route, grpclib.const.Cardinality.UNARY_UNARY, request_type, response_type
995+
route, grpclib.const.Cardinality.UNARY_UNARY, type(request), response_type
990996
) as stream:
991997
await stream.send_message(request, end=True)
992998
response = await stream.recv_message()
993999
assert response is not None
9941000
return response
9951001

9961002
async def _unary_stream(
997-
self, route: str, request_type: Type, response_type: Type[T], request: Any
1003+
self, route: str, request: "IProtoMessage", response_type: Type[T]
9981004
) -> AsyncGenerator[T, None]:
9991005
"""Make a unary request and return the stream response iterator."""
10001006
async with self.channel.request(
1001-
route, grpclib.const.Cardinality.UNARY_STREAM, request_type, response_type
1007+
route, grpclib.const.Cardinality.UNARY_STREAM, type(request), response_type
10021008
) as stream:
10031009
await stream.send_message(request, end=True)
10041010
async for message in stream:

betterproto/templates/template.py

Lines changed: 2 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)