diff --git a/requirements.txt b/requirements.txt --- a/requirements.txt +++ b/requirements.txt @@ -4,5 +4,5 @@ iso8601 beautifulsoup4 launchpadlib -tenacity +tenacity >= 6.2 xmltodict diff --git a/swh/lister/gitlab/lister.py b/swh/lister/gitlab/lister.py --- a/swh/lister/gitlab/lister.py +++ b/swh/lister/gitlab/lister.py @@ -17,7 +17,7 @@ from swh.lister import USER_AGENT from swh.lister.pattern import CredentialsType, Lister -from swh.lister.utils import is_retryable_exception, retry_attempt, throttling_retry +from swh.lister.utils import is_retryable_exception, throttling_retry from swh.scheduler.model import ListedOrigin logger = logging.getLogger(__name__) @@ -53,7 +53,7 @@ with specific ratelimit header. """ - attempt = retry_attempt(retry_state) + attempt = retry_state.outcome if attempt.failed: exc = attempt.exception() return ( diff --git a/swh/lister/pypi/lister.py b/swh/lister/pypi/lister.py --- a/swh/lister/pypi/lister.py +++ b/swh/lister/pypi/lister.py @@ -13,7 +13,7 @@ from tenacity.before_sleep import before_sleep_log -from swh.lister.utils import retry_attempt, throttling_retry +from swh.lister.utils import throttling_retry from swh.scheduler.interface import SchedulerInterface from swh.scheduler.model import ListedOrigin @@ -49,7 +49,7 @@ in 1 seconds.'> """ - attempt = retry_attempt(retry_state) + attempt = retry_state.outcome return attempt.failed and isinstance(attempt.exception(), Fault) diff --git a/swh/lister/tests/test_utils.py b/swh/lister/tests/test_utils.py --- a/swh/lister/tests/test_utils.py +++ b/swh/lister/tests/test_utils.py @@ -1,4 +1,4 @@ -# Copyright (C) 2018-2020 the Software Heritage developers +# Copyright (C) 2018-2021 the Software Heritage developers # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information @@ -47,14 +47,7 @@ def assert_sleep_calls(mocker, mock_sleep, sleep_params): - try: - mock_sleep.assert_has_calls([mocker.call(param) for param in sleep_params]) - except AssertionError: - # tenacity < 5.1 has a different behavior for wait_exponential - # https://github.com/jd/tenacity/commit/aac4307a0aa30d7befd0ebe4212ee4fc69083a95 - mock_sleep.assert_has_calls( - [mocker.call(param * WAIT_EXP_BASE) for param in sleep_params] - ) + mock_sleep.assert_has_calls([mocker.call(param) for param in sleep_params]) def test_throttling_retry(requests_mock, mocker): diff --git a/swh/lister/utils.py b/swh/lister/utils.py --- a/swh/lister/utils.py +++ b/swh/lister/utils.py @@ -55,24 +55,11 @@ return is_connection_error or is_throttling_exception(e) or is_500_error -def retry_attempt(retry_state): - """ - Utility function to get last retry attempt info based on the - tenacity version (as debian buster packages version 4.12). - """ - try: - attempt = retry_state.outcome - except AttributeError: - # tenacity < 5.0 - attempt = retry_state - return attempt - - def retry_if_exception(retry_state, predicate: Callable[[Exception], bool]) -> bool: """ Custom tenacity retry predicate for handling exceptions with the given predicate. """ - attempt = retry_attempt(retry_state) + attempt = retry_state.outcome if attempt.failed: exception = attempt.exception() return predicate(exception)