_typing.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # Copyright 2022 gRPC authors.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Common types for gRPC Sync API"""
  15. from typing import (TYPE_CHECKING, Any, Callable, Iterable, Iterator, Optional,
  16. Sequence, Tuple, TypeVar, Union)
  17. from grpc._cython import cygrpc
  18. if TYPE_CHECKING:
  19. from grpc import ServicerContext
  20. from grpc._server import _RPCState
  21. RequestType = TypeVar('RequestType')
  22. ResponseType = TypeVar('ResponseType')
  23. SerializingFunction = Callable[[Any], bytes]
  24. DeserializingFunction = Callable[[bytes], Any]
  25. MetadataType = Sequence[Tuple[str, Union[str, bytes]]]
  26. ChannelArgumentType = Tuple[str, Any]
  27. DoneCallbackType = Callable[[Any], None]
  28. NullaryCallbackType = Callable[[], None]
  29. RequestIterableType = Iterable[Any]
  30. ResponseIterableType = Iterable[Any]
  31. UserTag = Callable[[cygrpc.BaseEvent], bool]
  32. IntegratedCallFactory = Callable[[
  33. int, bytes, None, Optional[float], Optional[MetadataType], Optional[
  34. cygrpc.CallCredentials], Sequence[Sequence[cygrpc.
  35. Operation]], UserTag, Any
  36. ], cygrpc.IntegratedCall]
  37. ServerTagCallbackType = Tuple[Optional['_RPCState'],
  38. Sequence[NullaryCallbackType]]
  39. ServerCallbackTag = Callable[[cygrpc.BaseEvent], ServerTagCallbackType]
  40. ArityAgnosticMethodHandler = Union[
  41. Callable[[RequestType, 'ServicerContext', Callable[[ResponseType], None]],
  42. ResponseType],
  43. Callable[[RequestType, 'ServicerContext', Callable[[ResponseType], None]],
  44. Iterator[ResponseType]],
  45. Callable[[
  46. Iterator[RequestType], 'ServicerContext', Callable[[ResponseType], None]
  47. ], ResponseType], Callable[[
  48. Iterator[RequestType], 'ServicerContext', Callable[[ResponseType], None]
  49. ], Iterator[ResponseType]], Callable[[RequestType, 'ServicerContext'],
  50. ResponseType],
  51. Callable[[RequestType, 'ServicerContext'], Iterator[ResponseType]],
  52. Callable[[Iterator[RequestType], 'ServicerContext'],
  53. ResponseType], Callable[[Iterator[RequestType], 'ServicerContext'],
  54. Iterator[ResponseType]]]