1515
1616import logging
1717import time
18+ from typing import Any , AnyStr , Callable , Optional , Union
1819
1920import grpc
2021from grpc ._cython import cygrpc
22+ from grpc ._typing import DeserializingFunction
23+ from grpc ._typing import SerializingFunction
2124
2225_LOGGER = logging .getLogger (__name__ )
2326
6467 'GRPC_VERBOSITY=debug environment variable to see detailed error message.'
6568
6669
67- def encode (s ) :
70+ def encode (s : AnyStr ) -> bytes :
6871 if isinstance (s , bytes ):
6972 return s
7073 else :
7174 return s .encode ('utf8' )
7275
7376
74- def decode (b ) :
77+ def decode (b : AnyStr ) -> str :
7578 if isinstance (b , bytes ):
7679 return b .decode ('utf-8' , 'replace' )
7780 return b
7881
7982
80- def _transform (message , transformer , exception_message ):
83+ def _transform (message : Any , transformer : Union [SerializingFunction ,
84+ DeserializingFunction , None ],
85+ exception_message : str ) -> Any :
8186 if transformer is None :
8287 return message
8388 else :
@@ -88,26 +93,31 @@ def _transform(message, transformer, exception_message):
8893 return None
8994
9095
91- def serialize (message , serializer ) :
96+ def serialize (message : Any , serializer : Optional [ SerializingFunction ]) -> bytes :
9297 return _transform (message , serializer , 'Exception serializing message!' )
9398
9499
95- def deserialize (serialized_message , deserializer ):
100+ def deserialize (serialized_message : bytes ,
101+ deserializer : Optional [DeserializingFunction ]) -> Any :
96102 return _transform (serialized_message , deserializer ,
97103 'Exception deserializing message!' )
98104
99105
100- def fully_qualified_method (group , method ) :
106+ def fully_qualified_method (group : str , method : str ) -> str :
101107 return '/{}/{}' .format (group , method )
102108
103109
104- def _wait_once (wait_fn , timeout , spin_cb ):
110+ def _wait_once (wait_fn : Callable [..., None ], timeout : float ,
111+ spin_cb : Optional [Callable [[], None ]]):
105112 wait_fn (timeout = timeout )
106113 if spin_cb is not None :
107114 spin_cb ()
108115
109116
110- def wait (wait_fn , wait_complete_fn , timeout = None , spin_cb = None ):
117+ def wait (wait_fn : Callable [..., None ],
118+ wait_complete_fn : Callable [[], bool ],
119+ timeout : Optional [float ] = None ,
120+ spin_cb : Optional [Callable [[], None ]] = None ) -> bool :
111121 """Blocks waiting for an event without blocking the thread indefinitely.
112122
113123 See https://github.com/grpc/grpc/issues/19464 for full context. CPython's
@@ -148,7 +158,7 @@ def wait(wait_fn, wait_complete_fn, timeout=None, spin_cb=None):
148158 return False
149159
150160
151- def validate_port_binding_result (address , port ) :
161+ def validate_port_binding_result (address : str , port : int ) -> int :
152162 """Validates if the port binding succeed.
153163
154164 If the port returned by Core is 0, the binding is failed. However, in that
0 commit comments