Skip to content

Commit ac098a7

Browse files
daniel-sanchegcf-owl-bot[bot]parthea
authored
chore: move retry async check to wrap time (#649)
* chore: move retry async check to wrap time * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * added no cover mark --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Anthonios Partheniou <partheniou@google.com>
1 parent 1203fb9 commit ac098a7

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

google/api_core/retry/retry_unary.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,7 @@ def retry_target(
141141

142142
for sleep in sleep_generator:
143143
try:
144-
result = target()
145-
if inspect.isawaitable(result):
146-
warnings.warn(_ASYNC_RETRY_WARNING)
147-
return result
144+
return target()
148145

149146
# pylint: disable=broad-except
150147
# This function explicitly must deal with broad exceptions.
@@ -280,6 +277,8 @@ def __call__(
280277
Callable: A callable that will invoke ``func`` with retry
281278
behavior.
282279
"""
280+
if inspect.iscoroutinefunction(func):
281+
warnings.warn(_ASYNC_RETRY_WARNING)
283282
if self._on_error is not None:
284283
on_error = self._on_error
285284

tests/unit/retry/test_retry_unary.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,20 @@ def test_retry_target_non_retryable_error(utcnow, sleep):
101101
)
102102
@pytest.mark.asyncio
103103
async def test_retry_target_warning_for_retry(utcnow, sleep):
104-
predicate = retry.if_exception_type(ValueError)
105-
target = mock.AsyncMock(spec=["__call__"])
104+
"""
105+
retry.Retry should raise warning when wrapping an async function.
106+
"""
107+
108+
async def target():
109+
pass # pragma: NO COVER
110+
111+
retry_obj = retry.Retry()
106112

107113
with pytest.warns(Warning) as exc_info:
108-
# Note: predicate is just a filler and doesn't affect the test
109-
retry.retry_target(target, predicate, range(10), None)
114+
# raise warning when wrapping an async function
115+
retry_obj(target)
110116

111-
assert len(exc_info) == 2
117+
assert len(exc_info) == 1
112118
assert str(exc_info[0].message) == retry.retry_unary._ASYNC_RETRY_WARNING
113119
sleep.assert_not_called()
114120

0 commit comments

Comments
 (0)