Skip to content

Commit 3c69867

Browse files
partheadaniel-sanchegcf-owl-bot[bot]
authored
chore: move retry async check to wrap time (#668)
* 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: Daniel Sanche <[email protected]> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Daniel Sanche <[email protected]>
1 parent b91ed19 commit 3c69867

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
@@ -105,14 +105,20 @@ def test_retry_target_non_retryable_error(utcnow, sleep):
105105
)
106106
@pytest.mark.asyncio
107107
async def test_retry_target_warning_for_retry(utcnow, sleep):
108-
predicate = retry.if_exception_type(ValueError)
109-
target = mock.AsyncMock(spec=["__call__"])
108+
"""
109+
retry.Retry should raise warning when wrapping an async function.
110+
"""
111+
112+
async def target():
113+
pass # pragma: NO COVER
114+
115+
retry_obj = retry.Retry()
110116

111117
with pytest.warns(Warning) as exc_info:
112-
# Note: predicate is just a filler and doesn't affect the test
113-
retry.retry_target(target, predicate, range(10), None)
118+
# raise warning when wrapping an async function
119+
retry_obj(target)
114120

115-
assert len(exc_info) == 2
121+
assert len(exc_info) == 1
116122
assert str(exc_info[0].message) == retry.retry_unary._ASYNC_RETRY_WARNING
117123
sleep.assert_not_called()
118124

0 commit comments

Comments
 (0)