Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 87fa112

Browse files
committed
fix: add warning to retry target to avoid incorrect use
1 parent 405272c commit 87fa112

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

google/api_core/operations_v1/operations_async_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
from google.api_core import exceptions as core_exceptions
2828
from google.api_core import gapic_v1, page_iterator_async
29-
from google.api_core import retry as retries
29+
from google.api_core import retry_async as retries
3030
from google.api_core import timeout as timeouts
3131
from google.longrunning import operations_pb2
3232
from grpc import Compression
@@ -48,7 +48,7 @@ def __init__(self, channel, client_config=None):
4848
# Create the gRPC client stub with gRPC AsyncIO channel.
4949
self.operations_stub = operations_pb2.OperationsStub(channel)
5050

51-
default_retry = retries.Retry(
51+
default_retry = retries.AsyncRetry(
5252
initial=0.1, # seconds
5353
maximum=60.0, # seconds
5454
multiplier=1.3,

google/api_core/retry.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def check_if_exists():
6262
import random
6363
import sys
6464
import time
65+
import inspect
6566
from typing import Any, Callable, TypeVar, TYPE_CHECKING
6667

6768
import requests.exceptions
@@ -201,7 +202,10 @@ def retry_target(
201202

202203
for sleep in sleep_generator:
203204
try:
204-
return target()
205+
result = target()
206+
if inspect.isawaitable(result):
207+
raise exceptions.GoogleAPIError("Warning: Use google.api_core.retry_async.AsyncRetry for async calls!")
208+
return result
205209

206210
# pylint: disable=broad-except
207211
# This function explicitly must deal with broad exceptions.

tests/asyncio/test_retry_async.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
from google.api_core import exceptions
2222
from google.api_core import retry_async
23+
from google.api_core import retry
2324

2425

2526
@mock.patch("asyncio.sleep", autospec=True)
@@ -95,6 +96,24 @@ async def test_retry_target_non_retryable_error(utcnow, sleep):
9596
assert exc_info.value == exception
9697
sleep.assert_not_called()
9798

99+
@mock.patch("asyncio.sleep", autospec=True)
100+
@mock.patch(
101+
"google.api_core.datetime_helpers.utcnow",
102+
return_value=datetime.datetime.min,
103+
autospec=True,
104+
)
105+
@pytest.mark.asyncio
106+
async def test_retry_target_warning_for_retry(utcnow, sleep):
107+
predicate = retry.if_exception_type(exceptions.GoogleAPICallError)
108+
exception = exceptions.GoogleAPICallError("Warning: Use google.api_core.retry_async.AsyncRetry for async calls!")
109+
target = mock.Mock(side_effect=exception)
110+
111+
with pytest.raises(exceptions.GoogleAPICallError) as exc_info:
112+
retry.retry_target(target, predicate, range(10), None)
113+
114+
assert exc_info.value == exception
115+
sleep.assert_not_called()
116+
98117

99118
@mock.patch("asyncio.sleep", autospec=True)
100119
@mock.patch("google.api_core.datetime_helpers.utcnow", autospec=True)

0 commit comments

Comments
 (0)