tenacity
tenacity copied to clipboard
Change values for decorator in class object
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
changing-arguments-at-run-time may help you.
But if you need to use retry.statistics
,there are something wrong, look at #349