tenacity icon indicating copy to clipboard operation
tenacity copied to clipboard

retry_if_result always use None as a callback parameter

Open Draqun opened this issue 4 years ago • 1 comments

Hi. I couldn't find answer in documentation so I'm asking here. How to force attempt manager to put value returned by function into callback, when I iterate over Retrying. Example of code is below. In this example is_not_expected always has a None in value parameter. There is possibility to pass result variable content into is_not_expected via attempt/retryer?

from tenacity import Retrying, retry_if_result, stop_after_attempt, wait_fixed


def get_string():
    return 'NOT-TEST'


def is_not_expected(value):
    return value != 'TEST'


def main():
    retryer = Retrying(
        retry=retry_if_result(is_not_expected),
        stop=stop_after_attempt(3),
        wait=wait_fixed(0),
        reraise=True,
    )
    for attempt in retryer:
        with attempt:
            result = get_string()
    return result

main()

Draqun avatar Sep 08 '21 12:09 Draqun

Solution

from tenacity import Retrying, retry_if_result, stop_after_attempt, wait_fixed


def get_string():
    return 'NOT-TEST'


def is_not_expected(value):
    return value != 'TEST'


def main():
    retryer = Retrying(
        retry=retry_if_result(is_not_expected),
        stop=stop_after_attempt(3),
        wait=wait_fixed(0),
        reraise=True,
    )
    for attempt in retryer:
        with attempt:
            result = get_string()
        attempt.retry_state.set_result(result)  # SOLUTION
    return result

main()

Please add this example to documentation and I think it can be close.

Draqun avatar Oct 01 '21 11:10 Draqun