Skip to content

Commit

Permalink
fix: add warning to retry target to avoid incorrect use
Browse files Browse the repository at this point in the history
  • Loading branch information
ohmayr committed Oct 30, 2023
1 parent 405272c commit 663fb2f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
6 changes: 5 additions & 1 deletion google/api_core/retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def check_if_exists():
import random
import sys
import time
import inspect
from typing import Any, Callable, TypeVar, TYPE_CHECKING

import requests.exceptions
Expand Down Expand Up @@ -201,7 +202,10 @@ def retry_target(

for sleep in sleep_generator:
try:
return target()
result = target()
if inspect.isawaitable(result):
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.")
return result

# pylint: disable=broad-except
# This function explicitly must deal with broad exceptions.
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/test_retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,24 @@ def test_retry_target_non_retryable_error(utcnow, sleep):
assert exc_info.value == exception
sleep.assert_not_called()

@mock.patch("asyncio.sleep", autospec=True)
@mock.patch(
"google.api_core.datetime_helpers.utcnow",
return_value=datetime.datetime.min,
autospec=True,
)
@pytest.mark.asyncio
async def test_retry_target_warning_for_retry(utcnow, sleep):
predicate = retry.if_exception_type(ValueError)
target = mock.AsyncMock(spec=["__call__"])

with pytest.raises(exceptions.GoogleAPICallError) as exc_info:
retry.retry_target(target, predicate, range(10), None)

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.")
assert exc_info.type == exceptions.GoogleAPICallError
sleep.assert_not_called()


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

0 comments on commit 663fb2f

Please sign in to comment.