Skip to content

Commit

Permalink
[Python O11Y] Reapply registered method change (grpc#35850)
Browse files Browse the repository at this point in the history
This reverts commit a18279d.

<!--

If you know who should review your pull request, please assign it to that
person, otherwise the pull request would get assigned randomly.

If your pull request is for a specific language, please add the appropriate
lang label.

-->

Closes grpc#35850

PiperOrigin-RevId: 607476066
  • Loading branch information
XuanWang-Amos authored and copybara-github committed Feb 15, 2024
1 parent cf79445 commit 2c9b599
Show file tree
Hide file tree
Showing 46 changed files with 863 additions and 215 deletions.
27 changes: 14 additions & 13 deletions examples/python/helloworld/helloworld_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions examples/python/helloworld/helloworld_pb2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ from typing import ClassVar as _ClassVar, Optional as _Optional

DESCRIPTOR: _descriptor.FileDescriptor

class HelloReply(_message.Message):
__slots__ = ["message"]
MESSAGE_FIELD_NUMBER: _ClassVar[int]
message: str
def __init__(self, message: _Optional[str] = ...) -> None: ...

class HelloRequest(_message.Message):
__slots__ = ["name"]
__slots__ = ("name",)
NAME_FIELD_NUMBER: _ClassVar[int]
name: str
def __init__(self, name: _Optional[str] = ...) -> None: ...

class HelloReply(_message.Message):
__slots__ = ("message",)
MESSAGE_FIELD_NUMBER: _ClassVar[int]
message: str
def __init__(self, message: _Optional[str] = ...) -> None: ...
104 changes: 100 additions & 4 deletions examples/python/helloworld/helloworld_pb2_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,17 @@ def __init__(self, channel):
'/helloworld.Greeter/SayHello',
request_serializer=helloworld__pb2.HelloRequest.SerializeToString,
response_deserializer=helloworld__pb2.HelloReply.FromString,
)
_registered_method=True)
self.SayHelloStreamReply = channel.unary_stream(
'/helloworld.Greeter/SayHelloStreamReply',
request_serializer=helloworld__pb2.HelloRequest.SerializeToString,
response_deserializer=helloworld__pb2.HelloReply.FromString,
_registered_method=True)
self.SayHelloBidiStream = channel.stream_stream(
'/helloworld.Greeter/SayHelloBidiStream',
request_serializer=helloworld__pb2.HelloRequest.SerializeToString,
response_deserializer=helloworld__pb2.HelloReply.FromString,
_registered_method=True)


class GreeterServicer(object):
Expand All @@ -33,6 +43,18 @@ def SayHello(self, request, context):
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')

def SayHelloStreamReply(self, request, context):
"""Missing associated documentation comment in .proto file."""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')

def SayHelloBidiStream(self, request_iterator, context):
"""Missing associated documentation comment in .proto file."""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')


def add_GreeterServicer_to_server(servicer, server):
rpc_method_handlers = {
Expand All @@ -41,6 +63,16 @@ def add_GreeterServicer_to_server(servicer, server):
request_deserializer=helloworld__pb2.HelloRequest.FromString,
response_serializer=helloworld__pb2.HelloReply.SerializeToString,
),
'SayHelloStreamReply': grpc.unary_stream_rpc_method_handler(
servicer.SayHelloStreamReply,
request_deserializer=helloworld__pb2.HelloRequest.FromString,
response_serializer=helloworld__pb2.HelloReply.SerializeToString,
),
'SayHelloBidiStream': grpc.stream_stream_rpc_method_handler(
servicer.SayHelloBidiStream,
request_deserializer=helloworld__pb2.HelloRequest.FromString,
response_serializer=helloworld__pb2.HelloReply.SerializeToString,
),
}
generic_handler = grpc.method_handlers_generic_handler(
'helloworld.Greeter', rpc_method_handlers)
Expand All @@ -63,8 +95,72 @@ def SayHello(request,
wait_for_ready=None,
timeout=None,
metadata=None):
return grpc.experimental.unary_unary(request, target, '/helloworld.Greeter/SayHello',
return grpc.experimental.unary_unary(
request,
target,
'/helloworld.Greeter/SayHello',
helloworld__pb2.HelloRequest.SerializeToString,
helloworld__pb2.HelloReply.FromString,
options,
channel_credentials,
insecure,
call_credentials,
compression,
wait_for_ready,
timeout,
metadata,
_registered_method=True)

@staticmethod
def SayHelloStreamReply(request,
target,
options=(),
channel_credentials=None,
call_credentials=None,
insecure=False,
compression=None,
wait_for_ready=None,
timeout=None,
metadata=None):
return grpc.experimental.unary_stream(
request,
target,
'/helloworld.Greeter/SayHelloStreamReply',
helloworld__pb2.HelloRequest.SerializeToString,
helloworld__pb2.HelloReply.FromString,
options,
channel_credentials,
insecure,
call_credentials,
compression,
wait_for_ready,
timeout,
metadata,
_registered_method=True)

@staticmethod
def SayHelloBidiStream(request_iterator,
target,
options=(),
channel_credentials=None,
call_credentials=None,
insecure=False,
compression=None,
wait_for_ready=None,
timeout=None,
metadata=None):
return grpc.experimental.stream_stream(
request_iterator,
target,
'/helloworld.Greeter/SayHelloBidiStream',
helloworld__pb2.HelloRequest.SerializeToString,
helloworld__pb2.HelloReply.FromString,
options, channel_credentials,
insecure, call_credentials, compression, wait_for_ready, timeout, metadata)
options,
channel_credentials,
insecure,
call_credentials,
compression,
wait_for_ready,
timeout,
metadata,
_registered_method=True)
23 changes: 14 additions & 9 deletions src/compiler/python_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ bool PrivateGenerator::PrintStub(
out->Print(
method_dict,
"response_deserializer=$ResponseModuleAndClass$.FromString,\n");
out->Print(")\n");
out->Print("_registered_method=True)\n");
}
}
}
Expand Down Expand Up @@ -642,22 +642,27 @@ bool PrivateGenerator::PrintServiceClass(
args_dict["ArityMethodName"] = arity_method_name;
args_dict["PackageQualifiedService"] = package_qualified_service_name;
args_dict["Method"] = method->name();
out->Print(args_dict,
"return "
"grpc.experimental.$ArityMethodName$($RequestParameter$, "
"target, '/$PackageQualifiedService$/$Method$',\n");
out->Print(args_dict, "return grpc.experimental.$ArityMethodName$(\n");
{
IndentScope continuation_indent(out);
StringMap serializer_dict;
out->Print(args_dict, "$RequestParameter$,\n");
out->Print("target,\n");
out->Print(args_dict, "'/$PackageQualifiedService$/$Method$',\n");
serializer_dict["RequestModuleAndClass"] = request_module_and_class;
serializer_dict["ResponseModuleAndClass"] = response_module_and_class;
out->Print(serializer_dict,
"$RequestModuleAndClass$.SerializeToString,\n");
out->Print(serializer_dict, "$ResponseModuleAndClass$.FromString,\n");
out->Print("options, channel_credentials,\n");
out->Print(
"insecure, call_credentials, compression, wait_for_ready, "
"timeout, metadata)\n");
out->Print("options,\n");
out->Print("channel_credentials,\n");
out->Print("insecure,\n");
out->Print("call_credentials,\n");
out->Print("compression,\n");
out->Print("wait_for_ready,\n");
out->Print("timeout,\n");
out->Print("metadata,\n");
out->Print("_registered_method=True)\n");
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/python/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
gens/
build/
grpc_root/
third_party/
*_pb2.py
*_pb2.pyi
*_pb2_grpc.py
Expand Down
12 changes: 12 additions & 0 deletions src/python/grpcio/grpc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,7 @@ def unary_unary(
method,
request_serializer=None,
response_deserializer=None,
_registered_method=False,
):
"""Creates a UnaryUnaryMultiCallable for a unary-unary method.
Expand All @@ -1014,6 +1015,8 @@ def unary_unary(
response_deserializer: Optional :term:`deserializer` for deserializing the
response message. Response goes undeserialized in case None
is passed.
_registered_method: Implementation Private. A bool representing whether the method
is registered.
Returns:
A UnaryUnaryMultiCallable value for the named unary-unary method.
Expand All @@ -1026,6 +1029,7 @@ def unary_stream(
method,
request_serializer=None,
response_deserializer=None,
_registered_method=False,
):
"""Creates a UnaryStreamMultiCallable for a unary-stream method.
Expand All @@ -1036,6 +1040,8 @@ def unary_stream(
response_deserializer: Optional :term:`deserializer` for deserializing the
response message. Response goes undeserialized in case None is
passed.
_registered_method: Implementation Private. A bool representing whether the method
is registered.
Returns:
A UnaryStreamMultiCallable value for the name unary-stream method.
Expand All @@ -1048,6 +1054,7 @@ def stream_unary(
method,
request_serializer=None,
response_deserializer=None,
_registered_method=False,
):
"""Creates a StreamUnaryMultiCallable for a stream-unary method.
Expand All @@ -1058,6 +1065,8 @@ def stream_unary(
response_deserializer: Optional :term:`deserializer` for deserializing the
response message. Response goes undeserialized in case None is
passed.
_registered_method: Implementation Private. A bool representing whether the method
is registered.
Returns:
A StreamUnaryMultiCallable value for the named stream-unary method.
Expand All @@ -1070,6 +1079,7 @@ def stream_stream(
method,
request_serializer=None,
response_deserializer=None,
_registered_method=False,
):
"""Creates a StreamStreamMultiCallable for a stream-stream method.
Expand All @@ -1080,6 +1090,8 @@ def stream_stream(
response_deserializer: Optional :term:`deserializer` for deserializing the
response message. Response goes undeserialized in case None
is passed.
_registered_method: Implementation Private. A bool representing whether the method
is registered.
Returns:
A StreamStreamMultiCallable value for the named stream-stream method.
Expand Down
Loading

0 comments on commit 2c9b599

Please sign in to comment.