From 7ba5fe7cafce879120b8da26a1dff2334355a6ba Mon Sep 17 00:00:00 2001 From: Jacob Jensen Date: Tue, 14 Jan 2025 09:57:44 -0500 Subject: [PATCH 1/2] handle statistics update even if statistics dict not initialized --- tenacity/__init__.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tenacity/__init__.py b/tenacity/__init__.py index 72eba04..2c8b294 100644 --- a/tenacity/__init__.py +++ b/tenacity/__init__.py @@ -425,8 +425,10 @@ def next_action(rs: "RetryCallState") -> None: sleep = rs.upcoming_sleep rs.next_action = RetryAction(sleep) rs.idle_for += sleep - self.statistics["idle_for"] += sleep - self.statistics["attempt_number"] += 1 + self.statistics["idle_for"] = self.statistics.get("idle_for", 0) + sleep + self.statistics["attempt_number"] = ( + self.statistics.get("attempt_number", 0) + 1 + ) self._add_action_func(next_action) From f48d5e505489aacb0a2c17ef4798e290dba54ca3 Mon Sep 17 00:00:00 2001 From: Jacob Jensen Date: Tue, 14 Jan 2025 10:02:10 -0500 Subject: [PATCH 2/2] default attempt number 0 -> 1 --- tenacity/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tenacity/__init__.py b/tenacity/__init__.py index 2c8b294..ea72829 100644 --- a/tenacity/__init__.py +++ b/tenacity/__init__.py @@ -427,7 +427,7 @@ def next_action(rs: "RetryCallState") -> None: rs.idle_for += sleep self.statistics["idle_for"] = self.statistics.get("idle_for", 0) + sleep self.statistics["attempt_number"] = ( - self.statistics.get("attempt_number", 0) + 1 + self.statistics.get("attempt_number", 1) + 1 ) self._add_action_func(next_action)