Skip to content

Commit c9e1cbb

Browse files
chore: fallback to unary_unary when wrapping async callables (#653)
* fix: fallback to unary_unary when wrapping async callables We recently [made a change](ab22afd) to move some computation in the async rpc wrapper from call-time to wrap-time. This way, individual calls would execute faster, since they don't have to re-compute some data on each call A side-effect of this change is that now some [type validation](https://github.com/googleapis/python-api-core/blob/d96eb5cdd8120bfec97d62b09512c6fecc325be8/google/api_core/grpc_helpers_async.py#L209) happens earlier. This caused some downstream tests to fail when a mock grpc channel is used. The wrapper doesn't know how to handle the mock.Mock type, and raises an exception while constructing the client object This PR fixes the issue by falling back to the unary wrapper when the callable type is unknown, rather than raising an exception. This is in-line with how [the sync version handles it](https://github.com/googleapis/python-api-core/blob/d96eb5cdd8120bfec97d62b09512c6fecc325be8/google/api_core/grpc_helpers.py#L198) * fixed elif to if * removed outdated test
1 parent d96eb5c commit c9e1cbb

File tree

2 files changed

+3
-17
lines changed

2 files changed

+3
-17
lines changed

google/api_core/grpc_helpers_async.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,16 +197,15 @@ def wrap_errors(callable_):
197197
Returns: Callable: The wrapped gRPC callable.
198198
"""
199199
grpc_helpers._patch_callable_name(callable_)
200-
if isinstance(callable_, aio.UnaryUnaryMultiCallable):
201-
return _wrap_unary_errors(callable_)
202-
elif isinstance(callable_, aio.UnaryStreamMultiCallable):
200+
201+
if isinstance(callable_, aio.UnaryStreamMultiCallable):
203202
return _wrap_stream_errors(callable_, _WrappedUnaryStreamCall)
204203
elif isinstance(callable_, aio.StreamUnaryMultiCallable):
205204
return _wrap_stream_errors(callable_, _WrappedStreamUnaryCall)
206205
elif isinstance(callable_, aio.StreamStreamMultiCallable):
207206
return _wrap_stream_errors(callable_, _WrappedStreamStreamCall)
208207
else:
209-
raise TypeError("Unexpected type of callable: {}".format(type(callable_)))
208+
return _wrap_unary_errors(callable_)
210209

211210

212211
def create_channel(

tests/asyncio/test_grpc_helpers_async.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -165,19 +165,6 @@ async def test_wrap_stream_errors_stream_stream():
165165
assert mock_call.wait_for_connection.call_count == 1
166166

167167

168-
@pytest.mark.asyncio
169-
async def test_wrap_errors_type_error():
170-
"""
171-
If wrap_errors is called with an unexpected type, it should raise a TypeError.
172-
"""
173-
mock_call = mock.Mock()
174-
multicallable = mock.Mock(return_value=mock_call)
175-
176-
with pytest.raises(TypeError) as exc:
177-
grpc_helpers_async.wrap_errors(multicallable)
178-
assert "Unexpected type" in str(exc.value)
179-
180-
181168
@pytest.mark.asyncio
182169
async def test_wrap_stream_errors_raised():
183170
grpc_error = RpcErrorImpl(grpc.StatusCode.INVALID_ARGUMENT)

0 commit comments

Comments
 (0)