jd / jd/tenacity

Unreachable code in `before_log` reached

Open
#511 2 comments 1 reaction 0 assignees View on GitHub
Dominant language
Python
Stars
8.8k
Forks
359
Avg merge
1m
Merged PRs (30d)
1

Description

Hello, I've reached code that is marked as unreachable in `before_log` function:

```python
import logging

from tenacity import RetryError, Retrying, before_log

logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(levelname)s - %(message)s")
logger = logging.getLogger(__name__)

def f():
try:
for attempt in Retrying(
before=before_log(logger, logging.DEBUG),
):
with attempt:
raise Exception("hello")
except RetryError as exc:
exc.reraise()

f()
```

This code sample produces:
```
2025-01-28 15:11:42,725 - DEBUG - Starting call to '', this is the 72232nd time calling it.
2025-01-28 15:11:42,725 - DEBUG - Starting call to '', this is the 72233rd time calling it.
2025-01-28 15:11:42,725 - DEBUG - Starting call to '', this is the 72234th time calling it.
2025-01-28 15:11:42,725 - DEBUG - Starting call to '', this is the 72235th time calling it.
2025-01-28 15:11:42,725 - DEBUG - Starting call to '', this is the 72236th time calling it.
2025-01-28 15:11:42,725 - DEBUG - Starting call to '', this is the 72237th time calling it.
```

This is because `retry_state.fn` is probably not being set [here](https://github.com/jd/tenacity/blob/0d40e76f7d06d631fb127e1ec58c8bd776e70d49/tenacity/before.py#L38).

Python 3.9.19 & tenacity 9.0.0.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.