Skip to content

Commit 663fb2f

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

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

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.GoogleAPICallError("Warning: Using the synchronous google.api_core.retry.Retry with asynchronous calls may lead to unexpected results. Please use google.api_core.retry_async.AsyncRetry instead.")
208+
return result
205209

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

tests/unit/test_retry.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,24 @@ def test_retry_target_non_retryable_error(utcnow, sleep):
128128
assert exc_info.value == exception
129129
sleep.assert_not_called()
130130

131+
@mock.patch("asyncio.sleep", autospec=True)
132+
@mock.patch(
133+
"google.api_core.datetime_helpers.utcnow",
134+
return_value=datetime.datetime.min,
135+
autospec=True,
136+
)
137+
@pytest.mark.asyncio
138+
async def test_retry_target_warning_for_retry(utcnow, sleep):
139+
predicate = retry.if_exception_type(ValueError)
140+
target = mock.AsyncMock(spec=["__call__"])
141+
142+
with pytest.raises(exceptions.GoogleAPICallError) as exc_info:
143+
retry.retry_target(target, predicate, range(10), None)
144+
145+
assert exc_info.match("Warning: Using the synchronous google.api_core.retry.Retry with asynchronous calls may lead to unexpected results. Please use google.api_core.retry_async.AsyncRetry instead.")
146+
assert exc_info.type == exceptions.GoogleAPICallError
147+
sleep.assert_not_called()
148+
131149

132150
@mock.patch("time.sleep", autospec=True)
133151
@mock.patch("google.api_core.datetime_helpers.utcnow", autospec=True)

0 commit comments

Comments
 (0)