Skip to content

Commit 682b6d8

Browse files
committed
ALSO generate .pyi stubs, because I guess you can do that
1 parent 047dd58 commit 682b6d8

4 files changed

Lines changed: 66 additions & 9 deletions

File tree

protobuf-grpc/clients/client.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,13 @@
1616
import pb.employees.v1.employees_pb2 as employees_pb2
1717
import pb.employees.v1.employees_pb2_grpc as employees_pb2_grpc
1818

19+
# Note that if you did NOT specify --pyi_out in the protoc call, you won't get
20+
# editor help like tab completion for the generated code -- everything comes out
21+
# as a metaclass unless you also generate those .pyi files
1922
if __name__ == "__main__":
2023
with grpc.insecure_channel("127.0.0.1:8080") as channel:
2124
echo_stub = echo_pb2_grpc.EchoServiceStub(channel)
2225

23-
# This is pretty infuriating -- Python protobuf code isn't generated
24-
# directly, it generates a *Python* generator to generate the code at
25-
# runtime (in *_pb2.py). As such, you don't get completion hints in
26-
# editors because there's no data types to autofill etc. So, look at
27-
# your proto file and determine how you should be setting up your
28-
# requests!
2926
echo_request = echo_pb2.EchoRequest(msg = "Hello, gRPC!")
3027
echo_response = echo_stub.Echo(echo_request)
3128
print(echo_response)
@@ -35,6 +32,9 @@
3532
list_response = employees_stub.ListEmployees(list_request)
3633
print(list_response)
3734

38-
get_request = employees_pb2.GetEmployeeRequest(short_name = sys.argv[1])
39-
get_response = employees_stub.GetEmployee(get_request)
40-
print(get_response)
35+
if len(sys.argv) < 2:
36+
print("warning: not calling GetEmployee() because you must provide an employee's short name on the CLI. Did you look at the output of the ListEmployees() call?")
37+
else:
38+
get_request = employees_pb2.GetEmployeeRequest(short_name = sys.argv[1])
39+
get_response = employees_stub.GetEmployee(get_request)
40+
print(get_response)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from google.protobuf import descriptor as _descriptor
2+
from google.protobuf import message as _message
3+
from typing import ClassVar as _ClassVar, Optional as _Optional
4+
5+
DESCRIPTOR: _descriptor.FileDescriptor
6+
7+
class EchoRequest(_message.Message):
8+
__slots__ = ["msg"]
9+
MSG_FIELD_NUMBER: _ClassVar[int]
10+
msg: str
11+
def __init__(self, msg: _Optional[str] = ...) -> None: ...
12+
13+
class EchoResponse(_message.Message):
14+
__slots__ = ["msg"]
15+
MSG_FIELD_NUMBER: _ClassVar[int]
16+
msg: str
17+
def __init__(self, msg: _Optional[str] = ...) -> None: ...
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from google.api import annotations_pb2 as _annotations_pb2
2+
from google.protobuf.internal import containers as _containers
3+
from google.protobuf import descriptor as _descriptor
4+
from google.protobuf import message as _message
5+
from typing import ClassVar as _ClassVar, Iterable as _Iterable, Mapping as _Mapping, Optional as _Optional, Union as _Union
6+
7+
DESCRIPTOR: _descriptor.FileDescriptor
8+
9+
class Employee(_message.Message):
10+
__slots__ = ["birthday", "full_name", "id"]
11+
BIRTHDAY_FIELD_NUMBER: _ClassVar[int]
12+
FULL_NAME_FIELD_NUMBER: _ClassVar[int]
13+
ID_FIELD_NUMBER: _ClassVar[int]
14+
birthday: str
15+
full_name: str
16+
id: int
17+
def __init__(self, id: _Optional[int] = ..., full_name: _Optional[str] = ..., birthday: _Optional[str] = ...) -> None: ...
18+
19+
class GetEmployeeRequest(_message.Message):
20+
__slots__ = ["short_name"]
21+
SHORT_NAME_FIELD_NUMBER: _ClassVar[int]
22+
short_name: str
23+
def __init__(self, short_name: _Optional[str] = ...) -> None: ...
24+
25+
class GetEmployeeResponse(_message.Message):
26+
__slots__ = ["employee"]
27+
EMPLOYEE_FIELD_NUMBER: _ClassVar[int]
28+
employee: Employee
29+
def __init__(self, employee: _Optional[_Union[Employee, _Mapping]] = ...) -> None: ...
30+
31+
class ListEmployeesRequest(_message.Message):
32+
__slots__ = []
33+
def __init__(self) -> None: ...
34+
35+
class ListEmployeesResponse(_message.Message):
36+
__slots__ = ["short_names"]
37+
SHORT_NAMES_FIELD_NUMBER: _ClassVar[int]
38+
short_names: _containers.RepeatedScalarFieldContainer[str]
39+
def __init__(self, short_names: _Optional[_Iterable[str]] = ...) -> None: ...

protobuf-grpc/proto/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ generate: ensure-outdir
4848
--openapiv2_opt=logtostderr=true \
4949
--openapiv2_opt=generate_unbound_methods=true \
5050
--python_out=$(outdir) \
51+
--pyi_out=$(outdir) \
5152
--descriptor_set_out=./protoset \
5253
--include_imports \
5354
$(proto_files)

0 commit comments

Comments
 (0)