Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions tenacity/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ class LoggerProtocol(typing.Protocol):
Compatible with logging, structlog, loguru, etc...
"""

def log(
self, level: int, msg: str, *args: typing.Any, **kwargs: typing.Any
) -> typing.Any: ...
def log(self, level: int, msg: str, *args: typing.Any) -> typing.Any: ...
Copy link

Copilot AI Mar 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing **kwargs from LoggerProtocol.log() is a breaking API change for any users who have custom logger implementations typed against this protocol. The previous signature was def log(self, level: int, msg: str, *args: Any, **kwargs: Any) -> Any, which matched the broader logging.Logger.log signature that accepts stacklevel, extra, etc. as keyword arguments. Users who previously passed such keyword arguments through a LoggerProtocol-typed reference will now get a static type error. Consider whether a more conservative change (keeping **kwargs in the protocol even if tenacity itself no longer passes kwargs) would be preferable for backward compatibility.

Suggested change
def log(self, level: int, msg: str, *args: typing.Any) -> typing.Any: ...
def log(
self, level: int, msg: str, *args: typing.Any, **kwargs: typing.Any
) -> typing.Any: ...

Copilot uses AI. Check for mistakes.


def find_ordinal(pos_num: int) -> str:
Expand Down
23 changes: 11 additions & 12 deletions tenacity/before_sleep.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import traceback
import typing

from tenacity import _utils
Expand All @@ -35,8 +36,6 @@ def before_sleep_log(
"""Before sleep strategy that logs to some logger the attempt."""

def log_it(retry_state: "RetryCallState") -> None:
local_exc_info: BaseException | bool | None

if retry_state.outcome is None:
raise RuntimeError("log_it() called before outcome was set")

Expand All @@ -46,22 +45,22 @@ def log_it(retry_state: "RetryCallState") -> None:
if retry_state.outcome.failed:
ex = retry_state.outcome.exception()
verb, value = "raised", f"{ex.__class__.__name__}: {ex}"

if exc_info:
local_exc_info = retry_state.outcome.exception()
else:
local_exc_info = False
else:
verb, value = "returned", retry_state.outcome.result()
local_exc_info = False # exc_info does not apply when no exception

fn_name = retry_state.get_fn_name()

logger.log(
log_level,
msg = (
f"Retrying {fn_name} "
f"in {sec_format % retry_state.next_action.sleep} seconds as it {verb} {value}.",
exc_info=local_exc_info,
f"in {sec_format % retry_state.next_action.sleep} seconds as it {verb} {value}."
)

if exc_info and retry_state.outcome.failed:
ex = retry_state.outcome.exception()
Copy link

Copilot AI Mar 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ex variable is already assigned at line 46 when retry_state.outcome.failed is True. Since the guard condition on line 58 (if exc_info and retry_state.outcome.failed) ensures we are in the same failed branch, the re-assignment ex = retry_state.outcome.exception() on line 59 is a redundant duplicate call. The previously set ex from line 46 can be reused directly, eliminating the extra method call.

Suggested change
ex = retry_state.outcome.exception()

Copilot uses AI. Check for mistakes.
if ex is not None:
tb = "".join(traceback.format_exception(type(ex), ex, ex.__traceback__))
msg = f"{msg}\n{tb.rstrip()}"

logger.log(log_level, msg)

return log_it
Loading