tenacity
tenacity copied to clipboard
retry_if_result always use None as a callback parameter
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()
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.