tenacity icon indicating copy to clipboard operation
tenacity copied to clipboard

when Changing Arguments at Run Time with `retry_with`, `retry.statistics` return `{}`

Open xingdongzhe opened this issue 2 years ago • 1 comments

venv

  • Window 10
  • Python 3.9.7
  • tenacity 8.0.1

Here the reproduce code from Changing Arguments at Run Time:

@retry(stop=stop_after_attempt(3))
def raise_my_exception():
     print("raise_my_exception")
    raise Exception("Fail")

try:
    raise_my_exception.retry_with(stop=stop_after_attempt(4))()
except Exception:
    pass

print(raise_my_exception.retry.statistics)

Here what I got:

raise_my_exception
raise_my_exception
raise_my_exception
raise_my_exception
{}

Here I dig into to code what I got: https://github.com/jd/tenacity/blob/ff4843121c4763759d3d9806d18aaf642651a650/tenacity/init.py#L264-L275 when call retry_with(), it will return a new instance of BaseRetrying, and not copy of old one retry.statistics

xingdongzhe avatar Apr 05 '22 06:04 xingdongzhe

I made this clear: raise_my_exception.retry_with(stop=stop_after_attempt(4)) will return a new instance, the original raise_my_exception does not run, so it should be

try:
    new_instance = raise_my_exception.retry_with(stop=stop_after_attempt(4))
new_instance()
except Exception:
    pass

print(new_instance.retry.statistics)

This is docs mistakes

xingdongzhe avatar Jul 18 '23 09:07 xingdongzhe