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
8 changes: 8 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ repos:
rev: v2025.12.8.5
hooks:
- id: doccmd
name: Ruff format docs
alias: ruff-format-docs
args: ["--language", "python", "--no-pad-file", "--no-pad-groups", "--command", "ruff format", "docs/"]
additional_dependencies:
- ruff==0.14.8
- id: doccmd
name: Ruff check fix docs
alias: ruff-check-fix-docs
args: ["--language", "python", "--no-pad-file", "--no-pad-groups", "--command", "ruff check --fix", "docs/"]
additional_dependencies:
- ruff==0.14.8
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
### Internal

- Use Ruff to give the codebase a consistent format [#66](https://github.com/python-backoff/backoff/pull/66) (from [@edgarrmondragon](https://github.com/edgarrmondragon))
- Enable the `UP` (pyupgrade) Ruff rules and use Python 3.8+ code, including in docs [#73](https://github.com/python-backoff/backoff/pull/73) (from [@edgarrmondragon](https://github.com/edgarrmondragon))

## [v2.3.0] - 2025-11-28

Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ asynchronous HTTP client/server library.
max_time=60,
)
async def get_url(url):
async with aiohttp.ClientSession(raise_for_status=True) as session:
async with aiohttp.ClientSession(raise_for_status=True) as session: # noqa: SIM117
async with session.get(url) as response:
return await response.text()

Expand Down
1 change: 0 additions & 1 deletion backoff/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# coding:utf-8
"""
Function decoration for backoff and retry

Expand Down
1 change: 0 additions & 1 deletion backoff/_async.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# coding:utf-8
import asyncio
import functools
import inspect
Expand Down
2 changes: 0 additions & 2 deletions backoff/_common.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# coding:utf-8

import functools
import logging
import sys
Expand Down
1 change: 0 additions & 1 deletion backoff/_decorator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# coding:utf-8
import inspect
import logging
import operator
Expand Down
2 changes: 0 additions & 2 deletions backoff/_jitter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# coding:utf-8

import random


Expand Down
1 change: 0 additions & 1 deletion backoff/_sync.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# coding:utf-8
import functools
import time

Expand Down
15 changes: 1 addition & 14 deletions backoff/_typing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# coding:utf-8
import logging
import sys
from types import FunctionType
from typing import (
Any,
Expand All @@ -10,22 +8,11 @@
Generator,
Sequence,
Tuple,
TypedDict,
TypeVar,
Union,
)

if sys.version_info >= (3, 8): # pragma: no cover
from typing import TypedDict
else: # pragma: no cover
# use typing_extensions if installed but don't require it
try:
from typing_extensions import TypedDict
except ImportError:

class TypedDict(dict):
def __init_subclass__(cls, **kwargs: Any) -> None:
return super().__init_subclass__()


class _Details(TypedDict):
target: FunctionType
Expand Down
2 changes: 0 additions & 2 deletions backoff/_wait_gen.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# coding:utf-8

import itertools
import math
from typing import Any, Callable, Generator, Iterable, Optional, Union
Expand Down
1 change: 0 additions & 1 deletion backoff/types.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# coding:utf-8
from ._typing import Details

__all__ = [
Expand Down
1 change: 1 addition & 0 deletions docs/advanced/custom-strategies.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ A wait generator is a function that yields wait times in seconds:
```python
def my_wait_gen():
"""Yields: 1, 2, 3, 4, 5, 5, 5, ..."""
# Note: Using `yield from` can be unsafe in backoff wait generators.
for i in range(1, 6):
yield i
while True:
Expand Down
5 changes: 2 additions & 3 deletions docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,8 @@ import backoff
max_time=60,
)
async def fetch_async(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
return await response.json()
async with aiohttp.ClientSession() as session, session.get(url) as response:
return await response.json()
```

### Async Database Operations
Expand Down
5 changes: 2 additions & 3 deletions docs/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,8 @@ Yes! Just decorate async functions:
```python
@backoff.on_exception(backoff.expo, aiohttp.ClientError)
async def fetch_data(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
return await response.json()
async with aiohttp.ClientSession() as session, session.get(url) as response:
return await response.json()
```

### Can event handlers be async?
Expand Down
5 changes: 2 additions & 3 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,8 @@ import aiohttp
max_time=60,
)
async def get_url(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
return await response.text()
async with aiohttp.ClientSession() as session, session.get(url) as response:
return await response.text()
```

## Next Steps
Expand Down
11 changes: 5 additions & 6 deletions docs/user-guide/async.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ import aiohttp

@backoff.on_exception(backoff.expo, aiohttp.ClientError)
async def fetch_data(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
return await response.json()
async with aiohttp.ClientSession() as session, session.get(url) as response:
return await response.json()
```

## Async Event Handlers
Expand Down Expand Up @@ -47,7 +46,7 @@ async def async_operation():
max_time=60,
)
async def get_url(url):
async with aiohttp.ClientSession(raise_for_status=True) as session:
async with aiohttp.ClientSession(raise_for_status=True) as session: # noqa: SIM117
async with session.get(url) as response:
return await response.text()
```
Expand Down Expand Up @@ -100,7 +99,7 @@ async def fetch_all(urls):
max_time=300,
)
async def poll_job_status(job_id):
async with aiohttp.ClientSession() as session:
async with aiohttp.ClientSession() as session: # noqa: SIM117
async with session.get(f"/api/jobs/{job_id}") as response:
return await response.json()
```
Expand Down Expand Up @@ -169,7 +168,7 @@ async def log_async_retry(details):
on_backoff=log_async_retry,
)
async def robust_fetch(url, timeout=10):
async with aiohttp.ClientSession() as session:
async with aiohttp.ClientSession() as session: # noqa: SIM117
async with session.get(url, timeout=timeout) as response:
response.raise_for_status()
return await response.json()
Expand Down
2 changes: 1 addition & 1 deletion docs/user-guide/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ def should_retry(result):
return True
if not result.get("ready"):
return True
if result.get("status") == "processing":
if result.get("status") == "processing": # noqa: SIM103
return True
return False

Expand Down
5 changes: 2 additions & 3 deletions docs/user-guide/event-handlers.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,8 @@ def conditional_alert(details):
send_alert(f"High retry count: {details['tries']}")

# Only log errors, not warnings
if details.get("exception"):
if isinstance(details["exception"], CriticalError):
logger.error("Critical error during retry")
if details.get("exception") and isinstance(details["exception"], CriticalError):
logger.error("Critical error during retry")


@backoff.on_exception(
Expand Down
12 changes: 12 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,18 @@ docstring-code-line-length = 20
[tool.ruff.lint]
extend-select = [
"SIM", # flake8-simplify
"UP", # pyupgrade
]
ignore = [
# converting to a `yield from` expression is not safe because this library sends values via `send`
# https://docs.astral.sh/ruff/rules/yield-in-for-loop/#fix-safety
"UP028",
]

[tool.ruff.lint.per-file-ignores]
"**/doccmd_*.py" = [
"F811", # redefinition of unused
"F821", # undefined name
]

[tool.coverage.report]
Expand Down
1 change: 0 additions & 1 deletion tests/common.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# coding:utf-8
import collections
import functools

Expand Down
1 change: 0 additions & 1 deletion tests/test_backoff.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# coding:utf-8
import itertools
import logging
import random
Expand Down
2 changes: 0 additions & 2 deletions tests/test_backoff_async.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# coding:utf-8

import asyncio # Python 3.5 code and syntax is allowed in this file
import random

Expand Down
1 change: 0 additions & 1 deletion tests/test_jitter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# coding:utf-8
import backoff


Expand Down
2 changes: 0 additions & 2 deletions tests/test_types.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# coding:utf-8

from backoff.types import Details


Expand Down
1 change: 0 additions & 1 deletion tests/test_wait_gen.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# coding:utf-8
import backoff
import math

Expand Down
Loading