tenacity icon indicating copy to clipboard operation
tenacity copied to clipboard

Change values for decorator in class object

Open zacharyjones123 opened this issue 3 years ago • 1 comments

Hello all! I'm working on some code with Tenacity and have a question

For the below code, the only way to change the decorator is during instantiation time. After the TestClass object is created, the decorator can not be changed.

Is there a way that the retry options could be changed using the kwargs given by the user?

Main goal: Give the user of this class control of changing the retry options before the object is created

from tenacity import retry, wait_exponential, stop_after_delay, retry_if_exception_type

class TestClass:

    retry_options = {
        "wait": wait_exponential(multiplier=2, max=200),
        "stop": stop_after_delay(120),
        "retry": retry_if_exception_type(ValueError)
    }

    def __init__(**kwargs):
        for option in TestClass.retry_options:
            if option in kwargs:
                TestClass.retry_options[option] = kwargs.pop(option)


    @retry(**retry_options)
    def method_to_test():
        raise ValueError

client = TestClass(retry=retry_if_exception_type(NameError))
client.retry_options['retry'].exception_types
# Name Error
# However, the decorator for method_to_test does not change

zacharyjones123 avatar Sep 10 '21 02:09 zacharyjones123

changing-arguments-at-run-time may help you. But if you need to use retry.statistics,there are something wrong, look at #349

xingdongzhe avatar Apr 05 '22 06:04 xingdongzhe