_typing.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 (
  16. TYPE_CHECKING,
  17. Any,
  18. Callable,
  19. Iterable,
  20. Iterator,
  21. Optional,
  22. Sequence,
  23. Tuple,
  24. TypeVar,
  25. Union,
  26. )
  27. from grpc._cython import cygrpc
  28. if TYPE_CHECKING:
  29. from grpc import ServicerContext
  30. from grpc._server import _RPCState
  31. RequestType = TypeVar("RequestType")
  32. ResponseType = TypeVar("ResponseType")
  33. SerializingFunction = Callable[[Any], bytes]
  34. DeserializingFunction = Callable[[bytes], Any]
  35. MetadataType = Sequence[Tuple[str, Union[str, bytes]]]
  36. ChannelArgumentType = Tuple[str, Any]
  37. DoneCallbackType = Callable[[Any], None]
  38. NullaryCallbackType = Callable[[], None]
  39. RequestIterableType = Iterable[Any]
  40. ResponseIterableType = Iterable[Any]
  41. UserTag = Callable[[cygrpc.BaseEvent], bool]
  42. IntegratedCallFactory = Callable[
  43. [
  44. int,
  45. bytes,
  46. None,
  47. Optional[float],
  48. Optional[MetadataType],
  49. Optional[cygrpc.CallCredentials],
  50. Sequence[Sequence[cygrpc.Operation]],
  51. UserTag,
  52. Any,
  53. ],
  54. cygrpc.IntegratedCall,
  55. ]
  56. ServerTagCallbackType = Tuple[
  57. Optional["_RPCState"], Sequence[NullaryCallbackType]
  58. ]
  59. ServerCallbackTag = Callable[[cygrpc.BaseEvent], ServerTagCallbackType]
  60. ArityAgnosticMethodHandler = Union[
  61. Callable[
  62. [RequestType, "ServicerContext", Callable[[ResponseType], None]],
  63. ResponseType,
  64. ],
  65. Callable[
  66. [RequestType, "ServicerContext", Callable[[ResponseType], None]],
  67. Iterator[ResponseType],
  68. ],
  69. Callable[
  70. [
  71. Iterator[RequestType],
  72. "ServicerContext",
  73. Callable[[ResponseType], None],
  74. ],
  75. ResponseType,
  76. ],
  77. Callable[
  78. [
  79. Iterator[RequestType],
  80. "ServicerContext",
  81. Callable[[ResponseType], None],
  82. ],
  83. Iterator[ResponseType],
  84. ],
  85. Callable[[RequestType, "ServicerContext"], ResponseType],
  86. Callable[[RequestType, "ServicerContext"], Iterator[ResponseType]],
  87. Callable[[Iterator[RequestType], "ServicerContext"], ResponseType],
  88. Callable[
  89. [Iterator[RequestType], "ServicerContext"], Iterator[ResponseType]
  90. ],
  91. ]