GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix: add warning to retry target to avoid incorrect usage by ohmayr · Pull Request #543 · googleapis/python-api-core

This repository was archived by the owner on Feb 23, 2026. It is now read-only.

fix: add warning to retry target to avoid incorrect usage #543

Merged
ohmayr merged 4 commits into main from add-warning-to-retry
Nov 3, 2023

Uh oh!

There was an error while loading. Please reload this page.

Merged

fix: add warning to retry target to avoid incorrect usage #543

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
4 commits
Select commit Hold shift + click to select a range
5cdba7b
fix: add warning to retry target to avoid incorrect use
ohmayr Oct 26, 2023
08ddc27
fix: add comment for predicate
ohmayr Nov 3, 2023
489cea7
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Nov 3, 2023
5a6a503
Merge branch 'main' into add-warning-to-retry
ohmayr Nov 3, 2023
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

8 changes: 7 additions & 1 deletion 8 google/api_core/retry.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ def check_if_exists():
import random
import sys
import time
import inspect
import warnings
from typing import Any, Callable, TypeVar, TYPE_CHECKING

import requests.exceptions
Expand All @@ -84,6 +86,7 @@ def check_if_exists():
_DEFAULT_MAXIMUM_DELAY = 60.0 # seconds
_DEFAULT_DELAY_MULTIPLIER = 2.0
_DEFAULT_DEADLINE = 60.0 * 2.0 # seconds
_ASYNC_RETRY_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."


def if_exception_type(
Expand Down Expand Up @@ -201,7 +204,10 @@ def retry_target(

for sleep in sleep_generator:
try:
return target()
result = target()
if inspect.isawaitable(result):
warnings.warn(_ASYNC_RETRY_WARNING)
return result

# pylint: disable=broad-except
# This function explicitly must deal with broad exceptions.
Expand Down
20 changes: 20 additions & 0 deletions 20 tests/unit/test_retry.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,26 @@ def test_retry_target_non_retryable_error(utcnow, sleep):
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.warns(Warning) as exc_info:
# Note: predicate is just a filler and doesn't affect the test
retry.retry_target(target, predicate, range(10), None)
Comment thread
ohmayr marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.


assert len(exc_info) == 2
assert str(exc_info[0].message) == retry._ASYNC_RETRY_WARNING
sleep.assert_not_called()


@mock.patch("time.sleep", autospec=True)
@mock.patch("google.api_core.datetime_helpers.utcnow", autospec=True)
def test_retry_target_deadline_exceeded(utcnow, sleep):
Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.