[Python] Extend GRPC Typing (#9007)
Extend function calls with optional type infos for checking
and discovering.
https://github.com/grpc/grpc/blob/e838ba8a711ce838ccf3a2d472a20801a56dd615/src/python/grpcio/grpc/__init__.py#L680
diff --git a/grpc/src/compiler/python_generator.cc b/grpc/src/compiler/python_generator.cc
index 7e50266..bbd1bd5 100644
--- a/grpc/src/compiler/python_generator.cc
+++ b/grpc/src/compiler/python_generator.cc
@@ -172,6 +172,7 @@
<< " def __init__(self, channel: grpc.Channel) -> None: ...\n";
for (const RPCCall* method : service->calls.vec) {
+ imports->Import("typing");
std::string request = "bytes";
std::string response = "bytes";
@@ -183,14 +184,22 @@
imports->Import(ModuleFor(method->response), response);
}
- ss << " def " << method->name << "(self, ";
+ ss << " def " << method->name << "(\n";
+ ss << " self,\n";
if (ClientStreaming(method)) {
- imports->Import("typing");
- ss << "request_iterator: typing.Iterator[" << request << "]";
+ ss << " request_iterator: typing.Iterator[" << request << "]\n";
} else {
- ss << "request: " << request;
+ ss << " request: " << request << ",\n";
}
- ss << ") -> ";
+
+ ss << " timeout: float | None = None,\n";
+ // https://github.com/python/typeshed/blob/ccf9411fb1f5bee2a8e3d278889de17a08f7bbe3/stubs/grpcio/grpc/__init__.pyi#L37
+ ss << " metadata: typing.Sequence[tuple[str, typing.Union[str, bytes]]] | None = None,\n";
+ ss << " credentials: grpc.CallCredentials | None = None,\n";
+ ss << " wait_for_ready: bool | None = None,\n";
+ ss << " compression: grpc.Compression | None = None\n";
+
+ ss << " ) -> ";
if (ServerStreaming(method)) {
imports->Import("typing");
ss << "typing.Iterator[" << response << "]";