Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit11b5e02

Browse files
committed
fix: add warning to retry target to avoid incorrect use
1 parent405272c commit11b5e02

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

‎google/api_core/operations_v1/operations_async_client.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
fromgoogle.api_coreimportexceptionsascore_exceptions
2828
fromgoogle.api_coreimportgapic_v1,page_iterator_async
29-
fromgoogle.api_coreimportretryasretries
29+
fromgoogle.api_coreimportretry_asyncasretries
3030
fromgoogle.api_coreimporttimeoutastimeouts
3131
fromgoogle.longrunningimportoperations_pb2
3232
fromgrpcimportCompression
@@ -48,7 +48,7 @@ def __init__(self, channel, client_config=None):
4848
# Create the gRPC client stub with gRPC AsyncIO channel.
4949
self.operations_stub=operations_pb2.OperationsStub(channel)
5050

51-
default_retry=retries.Retry(
51+
default_retry=retries.AsyncRetry(
5252
initial=0.1,# seconds
5353
maximum=60.0,# seconds
5454
multiplier=1.3,

‎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
importrandom
6363
importsys
6464
importtime
65+
importinspect
6566
fromtypingimportAny,Callable,TypeVar,TYPE_CHECKING
6667

6768
importrequests.exceptions
@@ -201,7 +202,10 @@ def retry_target(
201202

202203
forsleepinsleep_generator:
203204
try:
204-
returntarget()
205+
result=target()
206+
ifinspect.isawaitable(result):
207+
raiseexceptions.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+
returnresult
205209

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

‎tests/asyncio/test_retry_async.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
fromgoogle.api_coreimportexceptions
2222
fromgoogle.api_coreimportretry_async
23+
fromgoogle.api_coreimportretry
2324

2425

2526
@mock.patch("asyncio.sleep",autospec=True)
@@ -95,6 +96,24 @@ async def test_retry_target_non_retryable_error(utcnow, sleep):
9596
assertexc_info.value==exception
9697
sleep.assert_not_called()
9798

99+
@mock.patch("asyncio.sleep",autospec=True)
100+
@mock.patch(
101+
"google.api_core.datetime_helpers.utcnow",
102+
return_value=datetime.datetime.min,
103+
autospec=True,
104+
)
105+
@pytest.mark.asyncio
106+
asyncdeftest_retry_target_warning_for_retry(utcnow,sleep):
107+
predicate=retry_async.if_exception_type(ValueError)
108+
target=mock.AsyncMock(spec=["__call__"])
109+
110+
withpytest.raises(exceptions.GoogleAPICallError)asexc_info:
111+
retry.retry_target(target,predicate,range(10),None)
112+
113+
assertexc_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.")
114+
assertexc_info.type==exceptions.GoogleAPICallError
115+
sleep.assert_not_called()
116+
98117

99118
@mock.patch("asyncio.sleep",autospec=True)
100119
@mock.patch("google.api_core.datetime_helpers.utcnow",autospec=True)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp