Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import gRPC stubs from the grpc-stubs project #11204

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update typing.Callable to collections.abc.Callable
  • Loading branch information
shabbyrobe committed Dec 27, 2023
commit 7a7cf619365a6b1e4ac014932afe12bf9a15ce48
55 changes: 30 additions & 25 deletions stubs/grpc/grpc/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import enum
import threading
import typing
from collections.abc import Callable
from concurrent import futures
from types import ModuleType, TracebackType
from typing_extensions import TypeAlias
Expand Down Expand Up @@ -41,6 +42,20 @@ Metadata: TypeAlias = tuple[tuple[str, str | bytes], ...]
TRequest = typing.TypeVar("TRequest")
TResponse = typing.TypeVar("TResponse")

# XXX: These are probably the SerializeToTring/FromString pb2 methods, but
# this needs further investigation
class RequestSerializer(typing.Protocol):
def __call__(self, *args: typing.Any, **kwargs: typing.Any): ...

class RequestDeserializer(typing.Protocol):
def __call__(self, *args: typing.Any, **kwargs: typing.Any): ...

class ResponseSerializer(typing.Protocol):
def __call__(self, *args: typing.Any, **kwargs: typing.Any): ...

class ResponseDeserializer(typing.Protocol):
def __call__(self, *args: typing.Any, **kwargs: typing.Any): ...

# Create Client:

def insecure_channel(target: str, options: _Options | None = ..., compression: Compression | None = ...) -> Channel: ...
Expand Down Expand Up @@ -104,7 +119,7 @@ def ssl_server_certificate_configuration(
) -> ServerCertificateConfiguration: ...
def dynamic_ssl_server_credentials(
initial_certificate_configuration: ServerCertificateConfiguration,
certificate_configuration_fetcher: typing.Callable[[], ServerCertificateConfiguration],
certificate_configuration_fetcher: Callable[[], ServerCertificateConfiguration],
require_client_authentication: bool = ...,
) -> ServerCredentials: ...
def alts_server_credentials() -> ServerCredentials: ...
Expand All @@ -120,12 +135,7 @@ def xds_server_credentials(fallback_credentials: ServerCredentials) -> ServerCre
# def FloobDoob(self, request, context):
# return response
#
Behaviour: TypeAlias = typing.Callable

# XXX: These are probably the SerializeToTring/FromString pb2 methods, but
# this needs further investigation
RequestDeserialize: TypeAlias = typing.Callable
ResponseSerialize: TypeAlias = typing.Callable
Behaviour: TypeAlias = Callable

def unary_unary_rpc_method_handler(
behavior: Behaviour,
Expand Down Expand Up @@ -194,11 +204,6 @@ class StatusCode(enum.Enum):

# Channel Object:

# XXX: These are probably the SerializeToTring/FromString pb2 methods, but
# this needs further investigation
RequestSerializer: TypeAlias = typing.Callable
ResponseDeserializer: TypeAlias = typing.Callable

class Channel:
def close(self) -> None: ...
def stream_stream(
Expand All @@ -207,14 +212,14 @@ class Channel:
def stream_unary(
self, method: str, request_serializer: RequestSerializer | None, response_deserializer: ResponseDeserializer | None
) -> StreamUnaryMultiCallable: ...
def subscribe(self, callback: typing.Callable[[ChannelConnectivity], None], try_to_connect: bool = ...) -> None: ...
def subscribe(self, callback: Callable[[ChannelConnectivity], None], try_to_connect: bool = ...) -> None: ...
def unary_stream(
self, method: str, request_serializer: RequestSerializer | None, response_deserializer: ResponseDeserializer | None
) -> UnaryStreamMultiCallable: ...
def unary_unary(
self, method: str, request_serializer: RequestSerializer | None, response_deserializer: ResponseDeserializer | None
) -> UnaryUnaryMultiCallable: ...
def unsubscribe(self, callback: typing.Callable[[ChannelConnectivity], None]) -> None: ...
def unsubscribe(self, callback: Callable[[ChannelConnectivity], None]) -> None: ...
def __enter__(self) -> Channel: ...
def __exit__(
self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None
Expand Down Expand Up @@ -284,7 +289,7 @@ class RpcError(Exception):
# Shared Context:

class RpcContext:
def add_callback(self, callback: typing.Callable[[], None]) -> bool: ...
def add_callback(self, callback: Callable[[], None]) -> bool: ...
def cancel(self): ...
def is_active(self) -> bool: ...
def time_remaining(self) -> float: ...
Expand Down Expand Up @@ -337,7 +342,7 @@ class UnaryUnaryClientInterceptor(typing.Generic[TRequest, TResponse]):
# status, the returned Call-Future’s exception value will be an
# RpcError.
#
continuation: typing.Callable[[ClientCallDetails, TRequest], CallFuture[TResponse]],
continuation: Callable[[ClientCallDetails, TRequest], CallFuture[TResponse]],
client_call_details: ClientCallDetails,
request: TRequest,
) -> CallFuture[TResponse]: ...
Expand All @@ -348,23 +353,23 @@ class CallIterator(Call, typing.Generic[TResponse]):
class UnaryStreamClientInterceptor(typing.Generic[TRequest, TResponse]):
def intercept_unary_stream(
self,
continuation: typing.Callable[[ClientCallDetails, TRequest], CallIterator[TResponse]],
continuation: Callable[[ClientCallDetails, TRequest], CallIterator[TResponse]],
client_call_details: ClientCallDetails,
request: TRequest,
) -> CallIterator[TResponse]: ...

class StreamUnaryClientInterceptor(typing.Generic[TRequest, TResponse]):
def intercept_stream_unary(
self,
continuation: typing.Callable[[ClientCallDetails, TRequest], CallFuture[TResponse]],
continuation: Callable[[ClientCallDetails, TRequest], CallFuture[TResponse]],
client_call_details: ClientCallDetails,
request_iterator: typing.Iterator[TRequest],
) -> CallFuture[TResponse]: ...

class StreamStreamClientInterceptor(typing.Generic[TRequest, TResponse]):
def intercept_stream_stream(
self,
continuation: typing.Callable[[ClientCallDetails, TRequest], CallIterator[TResponse]],
continuation: Callable[[ClientCallDetails, TRequest], CallIterator[TResponse]],
client_call_details: ClientCallDetails,
request_iterator: typing.Iterator[TRequest],
) -> CallIterator[TResponse]: ...
Expand Down Expand Up @@ -405,13 +410,13 @@ class RpcMethodHandler(typing.Generic[TRequest, TResponse]):
# XXX: not clear from docs whether this is optional or not
response_serializer: ResponseSerializer | None

unary_unary: typing.Callable[[TRequest, ServicerContext], TResponse] | None
unary_unary: Callable[[TRequest, ServicerContext], TResponse] | None

unary_stream: typing.Callable[[TRequest, ServicerContext], typing.Iterator[TResponse]] | None
unary_stream: Callable[[TRequest, ServicerContext], typing.Iterator[TResponse]] | None

stream_unary: typing.Callable[[typing.Iterator[TRequest], ServicerContext], TResponse] | None
stream_unary: Callable[[typing.Iterator[TRequest], ServicerContext], TResponse] | None

stream_stream: typing.Callable[[typing.Iterator[TRequest], ServicerContext], typing.Iterator[TResponse]] | None
stream_stream: Callable[[typing.Iterator[TRequest], ServicerContext], typing.Iterator[TResponse]] | None

class HandlerCallDetails:
method: str
Expand All @@ -428,7 +433,7 @@ class ServiceRpcHandler(GenericRpcHandler[TRequest, TResponse], typing.Generic[T
class ServerInterceptor(typing.Generic[TRequest, TResponse]):
def intercept_service(
self,
continuation: typing.Callable[[HandlerCallDetails], RpcMethodHandler[TRequest, TResponse] | None],
continuation: Callable[[HandlerCallDetails], RpcMethodHandler[TRequest, TResponse] | None],
handler_call_details: HandlerCallDetails,
) -> RpcMethodHandler[TRequest, TResponse] | None: ...

Expand Down Expand Up @@ -534,7 +539,7 @@ class FutureCancelledError(Exception): ...
TFutureValue = typing.TypeVar("TFutureValue")

class Future(typing.Generic[TFutureValue]):
def add_done_callback(self, fn: typing.Callable[[Future[TFutureValue]], None]) -> None: ...
def add_done_callback(self, fn: Callable[[Future[TFutureValue]], None]) -> None: ...
def cancel(self) -> bool: ...
def cancelled(self) -> bool: ...
def done(self) -> bool: ...
Expand Down
17 changes: 9 additions & 8 deletions stubs/grpc/grpc/aio/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import typing
from collections.abc import Callable
from concurrent import futures
from types import TracebackType
from typing_extensions import TypeAlias
Expand Down Expand Up @@ -75,8 +76,8 @@ def server(
# XXX: The docs suggest these type signatures for aio, but not for non-async,
# and it's unclear why;
# https://grpc.github.io/grpc/python/grpc_asyncio.html#grpc.aio.Channel.stream_stream
RequestSerializer: TypeAlias = typing.Callable[[typing.Any], bytes]
ResponseDeserializer: TypeAlias = typing.Callable[[bytes], typing.Any]
RequestSerializer: TypeAlias = Callable[[typing.Any], bytes]
ResponseDeserializer: TypeAlias = Callable[[bytes], typing.Any]

class Channel:
async def close(self, grace: float | None) -> None: ...
Expand Down Expand Up @@ -122,7 +123,7 @@ class Server:

# Client-Side Context:

DoneCallbackType: TypeAlias = typing.Callable[[typing.Any], None]
DoneCallbackType: TypeAlias = Callable[[typing.Any], None]
EOFType: TypeAlias = object

class RpcContext:
Expand Down Expand Up @@ -262,31 +263,31 @@ class UnaryUnaryClientInterceptor(typing.Generic[TRequest, TResponse]):
async def intercept_unary_unary(
self,
# XXX: See equivalent function in grpc types for notes about continuation:
continuation: typing.Callable[[ClientCallDetails, TRequest], UnaryUnaryCall[TRequest, TResponse]],
continuation: Callable[[ClientCallDetails, TRequest], UnaryUnaryCall[TRequest, TResponse]],
client_call_details: ClientCallDetails,
request: TRequest,
) -> TResponse: ...

class UnaryStreamClientInterceptor(typing.Generic[TRequest, TResponse]):
async def intercept_unary_stream(
self,
continuation: typing.Callable[[ClientCallDetails, TRequest], UnaryStreamCall[TRequest, TResponse]],
continuation: Callable[[ClientCallDetails, TRequest], UnaryStreamCall[TRequest, TResponse]],
client_call_details: ClientCallDetails,
request: TRequest,
) -> typing.AsyncIterable[TResponse] | UnaryStreamCall[TRequest, TResponse]: ...

class StreamUnaryClientInterceptor(typing.Generic[TRequest, TResponse]):
async def intercept_stream_unary(
self,
continuation: typing.Callable[[ClientCallDetails, TRequest], StreamUnaryCall[TRequest, TResponse]],
continuation: Callable[[ClientCallDetails, TRequest], StreamUnaryCall[TRequest, TResponse]],
client_call_details: ClientCallDetails,
request_iterator: typing.AsyncIterable[TRequest] | typing.Iterable[TRequest],
) -> typing.AsyncIterable[TResponse] | UnaryStreamCall[TRequest, TResponse]: ...

class StreamStreamClientInterceptor(typing.Generic[TRequest, TResponse]):
async def intercept_stream_stream(
self,
continuation: typing.Callable[[ClientCallDetails, TRequest], StreamStreamCall[TRequest, TResponse]],
continuation: Callable[[ClientCallDetails, TRequest], StreamStreamCall[TRequest, TResponse]],
client_call_details: ClientCallDetails,
request_iterator: typing.AsyncIterable[TRequest] | typing.Iterable[TRequest],
) -> typing.AsyncIterable[TResponse] | StreamStreamCall[TRequest, TResponse]: ...
Expand All @@ -296,7 +297,7 @@ class StreamStreamClientInterceptor(typing.Generic[TRequest, TResponse]):
class ServerInterceptor(typing.Generic[TRequest, TResponse]):
async def intercept_service(
self,
continuation: typing.Callable[[HandlerCallDetails], typing.Awaitable[RpcMethodHandler[TRequest, TResponse]]],
continuation: Callable[[HandlerCallDetails], typing.Awaitable[RpcMethodHandler[TRequest, TResponse]]],
handler_call_details: HandlerCallDetails,
) -> RpcMethodHandler[TRequest, TResponse]: ...

Expand Down