Skip to content

Commit 3298f4b

Browse files
aidandjWouldYouKindly
authored andcommitted
add constructor typing
Signed-off-by: Aidan Jensen <aidandj.github@gmail.com>
1 parent 28d2401 commit 3298f4b

File tree

7 files changed

+30
-3
lines changed

7 files changed

+30
-3
lines changed

mypy_protobuf/main.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,18 @@ def write_grpc_services(
10221022
self._import("grpc.aio", "Channel"),
10231023
)
10241024
wl("")
1025+
elif self.grpc_type == GRPCType.SYNC:
1026+
wl(
1027+
"def __init__(self, channel: {}) -> None: ...",
1028+
self._import("grpc", "Channel"),
1029+
)
1030+
wl("")
1031+
elif self.grpc_type == GRPCType.ASYNC:
1032+
wl(
1033+
"def __init__(self, channel: {}) -> None: ...",
1034+
self._import("grpc.aio", "Channel"),
1035+
)
1036+
wl("")
10251037

10261038
self.write_grpc_stub_methods(service, scl)
10271039

test/async_only/test_async_only.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ async def UnaryUnary(
2424
return dummy_pb2.DummyReply(value=request.value[::-1])
2525

2626

27-
async def main():
27+
async def noop() -> None:
28+
"""Don't actually run anything; this is just for type-checking."""
2829
stub = dummy_pb2_grpc.DummyServiceStub(channel=grpc.aio.insecure_channel("localhost:50051"))
2930
await stub.UnaryUnary(dummy_pb2.DummyRequest(value="test"))

test/generated_async_only/testproto/grpc/dummy_pb2_grpc.pyi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ _DummyServiceStreamStreamType: typing_extensions.TypeAlias = grpc.aio.StreamStre
4242
class DummyServiceStub:
4343
"""DummyService"""
4444

45+
def __init__(self, channel: grpc.aio.Channel) -> None: ...
46+
4547
UnaryUnary: _DummyServiceUnaryUnaryType
4648
"""UnaryUnary"""
4749

@@ -103,6 +105,8 @@ _DeprecatedServiceDeprecatedMethodNotDeprecatedRequestType: typing_extensions.Ty
103105
class DeprecatedServiceStub:
104106
"""Marking the service as deprecated"""
105107

108+
def __init__(self, channel: grpc.aio.Channel) -> None: ...
109+
106110
DeprecatedMethod: _DeprecatedServiceDeprecatedMethodType
107111
"""DeprecatedMethod"""
108112

test/generated_async_only/testproto/grpc/import_pb2_grpc.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ _SimpleServiceNoCommentType: typing_extensions.TypeAlias = grpc.aio.UnaryUnaryMu
3434
class SimpleServiceStub:
3535
"""SimpleService"""
3636

37+
def __init__(self, channel: grpc.aio.Channel) -> None: ...
38+
3739
UnaryUnary: _SimpleServiceUnaryUnaryType
3840
"""UnaryUnary"""
3941

test/generated_sync_only/testproto/grpc/dummy_pb2_grpc.pyi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ _DummyServiceStreamStreamType: typing_extensions.TypeAlias = grpc.StreamStreamMu
4242
class DummyServiceStub:
4343
"""DummyService"""
4444

45+
def __init__(self, channel: grpc.Channel) -> None: ...
46+
4547
UnaryUnary: _DummyServiceUnaryUnaryType
4648
"""UnaryUnary"""
4749

@@ -103,6 +105,8 @@ _DeprecatedServiceDeprecatedMethodNotDeprecatedRequestType: typing_extensions.Ty
103105
class DeprecatedServiceStub:
104106
"""Marking the service as deprecated"""
105107

108+
def __init__(self, channel: grpc.Channel) -> None: ...
109+
106110
DeprecatedMethod: _DeprecatedServiceDeprecatedMethodType
107111
"""DeprecatedMethod"""
108112

test/generated_sync_only/testproto/grpc/import_pb2_grpc.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ _SimpleServiceNoCommentType: typing_extensions.TypeAlias = grpc.UnaryUnaryMultiC
3434
class SimpleServiceStub:
3535
"""SimpleService"""
3636

37+
def __init__(self, channel: grpc.Channel) -> None: ...
38+
3739
UnaryUnary: _SimpleServiceUnaryUnaryType
3840
"""UnaryUnary"""
3941

test/sync_only/test_sync_only.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,7 @@ def UnaryUnary(
2121
return dummy_pb2.DummyReply(value=request.value[::-1])
2222

2323

24-
stub = dummy_pb2_grpc.DummyServiceStub(channel=grpc.insecure_channel("localhost:50051"))
25-
stub.UnaryUnary(dummy_pb2.DummyRequest(value="test"))
24+
def noop() -> None:
25+
"""Don't actually run anything; this is just for type-checking."""
26+
stub = dummy_pb2_grpc.DummyServiceStub(channel=grpc.insecure_channel("localhost:50051"))
27+
stub.UnaryUnary(dummy_pb2.DummyRequest(value="test"))

0 commit comments

Comments
 (0)