-
-
Notifications
You must be signed in to change notification settings - Fork 283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add async strategies #451
Merged
Merged
Add async strategies #451
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
df6d341
Add async strategies
hasier 84fe208
Fix init typing
hasier e145ef3
Reuse is_coroutine_callable
hasier c97c138
Keep only async predicate overrides and DRY implementations
hasier d0f11cb
Ensure async and/or versions called when necessary
hasier 3c5f788
Run ruff format
hasier 927965d
Copy over strategies as async
hasier 4aa9ccf
Add release note
hasier File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
# Copyright 2016–2021 Julien Danjou | ||
# Copyright 2016 Joshua Harlow | ||
# Copyright 2013-2014 Ray Holder | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import abc | ||
import inspect | ||
import typing | ||
|
||
from tenacity import _utils | ||
from tenacity import retry_base | ||
from tenacity import retry_if_exception as _retry_if_exception | ||
from tenacity import retry_if_result as _retry_if_result | ||
|
||
if typing.TYPE_CHECKING: | ||
from tenacity import RetryCallState | ||
|
||
|
||
class async_retry_base(retry_base): | ||
"""Abstract base class for async retry strategies.""" | ||
|
||
@abc.abstractmethod | ||
async def __call__(self, retry_state: "RetryCallState") -> bool: # type: ignore[override] | ||
pass | ||
|
||
def __and__( # type: ignore[override] | ||
self, other: "typing.Union[retry_base, async_retry_base]" | ||
) -> "retry_all": | ||
return retry_all(self, other) | ||
|
||
def __rand__( # type: ignore[misc,override] | ||
self, other: "typing.Union[retry_base, async_retry_base]" | ||
) -> "retry_all": | ||
return retry_all(other, self) | ||
|
||
def __or__( # type: ignore[override] | ||
self, other: "typing.Union[retry_base, async_retry_base]" | ||
) -> "retry_any": | ||
return retry_any(self, other) | ||
|
||
def __ror__( # type: ignore[misc,override] | ||
self, other: "typing.Union[retry_base, async_retry_base]" | ||
) -> "retry_any": | ||
return retry_any(other, self) | ||
|
||
|
||
class async_predicate_mixin: | ||
async def __call__(self, retry_state: "RetryCallState") -> bool: | ||
result = super().__call__(retry_state) # type: ignore[misc] | ||
if inspect.isawaitable(result): | ||
result = await result | ||
return typing.cast(bool, result) | ||
|
||
|
||
RetryBaseT = typing.Union[ | ||
async_retry_base, typing.Callable[["RetryCallState"], typing.Awaitable[bool]] | ||
] | ||
|
||
|
||
class retry_if_exception(async_predicate_mixin, _retry_if_exception, async_retry_base): # type: ignore[misc] | ||
"""Retry strategy that retries if an exception verifies a predicate.""" | ||
|
||
def __init__( | ||
self, predicate: typing.Callable[[BaseException], typing.Awaitable[bool]] | ||
) -> None: | ||
super().__init__(predicate) # type: ignore[arg-type] | ||
|
||
|
||
class retry_if_result(async_predicate_mixin, _retry_if_result, async_retry_base): # type: ignore[misc] | ||
"""Retries if the result verifies a predicate.""" | ||
|
||
def __init__( | ||
self, predicate: typing.Callable[[typing.Any], typing.Awaitable[bool]] | ||
) -> None: | ||
super().__init__(predicate) # type: ignore[arg-type] | ||
|
||
|
||
class retry_any(async_retry_base): | ||
"""Retries if any of the retries condition is valid.""" | ||
|
||
def __init__(self, *retries: typing.Union[retry_base, async_retry_base]) -> None: | ||
self.retries = retries | ||
|
||
async def __call__(self, retry_state: "RetryCallState") -> bool: # type: ignore[override] | ||
result = False | ||
for r in self.retries: | ||
result = result or await _utils.wrap_to_async_func(r)(retry_state) | ||
if result: | ||
break | ||
return result | ||
|
||
|
||
class retry_all(async_retry_base): | ||
"""Retries if all the retries condition are valid.""" | ||
|
||
def __init__(self, *retries: typing.Union[retry_base, async_retry_base]) -> None: | ||
self.retries = retries | ||
|
||
async def __call__(self, retry_state: "RetryCallState") -> bool: # type: ignore[override] | ||
result = True | ||
for r in self.retries: | ||
result = result and await _utils.wrap_to_async_func(r)(retry_state) | ||
if not result: | ||
break | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this really needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not strictly, but it helps with the typing, otherwise
mypy
and similar tools would complain about async functions.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, that does not look good though.
I think
self.predicate
is going to have different types than the one defined inretry_base
. I think we might need to not useretry_base
or simply use a different async base.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's fair. I mainly wanted to keep it dry and reuse as much as possible. Would you rather have a whole new (mainly copy-paste) async implementation for these cases then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it'd be better. As we started to type everything, I know from experience that trying to be smarter than mypy ends up haunting you at some point.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hahah fair, done! 927965d