Skip to content
This repository was archived by the owner on Aug 8, 2025. It is now read-only.
Open
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
55 changes: 55 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: CI

on:
push:
branches:
- main
pull_request:
branches:
- main
workflow_dispatch:
schedule:
# Daily at 12:00 UTC
- cron: "0 12 * * *"

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

env:
FORCE_COLOR: 1

jobs:
test:
name: Python ${{ matrix.python-version }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-24.04]
python-version:
- "3.8"
- "3.9"
- "3.10"
- "3.11"
- "3.12"
- "3.13"
- "3.14"
include:
- os: ubuntu-22.04
python-version: "3.7"
steps:
- name: Checkout code
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0

- name: Set up Python
uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
with:
python-version: ${{ matrix.python-version }}
allow-prereleases: true

- name: Install uv
uses: astral-sh/setup-uv@d9e0f98d3fc6adb07d1e3d37f3043649ddad06a1 # v6.5.0

- name: Run tests
run: uvx --with tox-uv tox -e ${{ matrix.python-version }}
8 changes: 4 additions & 4 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v2
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
with:
# We must fetch at least the immediate parents so that if this is
# a pull request then we can checkout the head.
Expand All @@ -43,7 +43,7 @@ jobs:

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
uses: github/codeql-action/init@96f518a34f7a870018057716cc4d7a5c014bd61c # v3.29.10
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
Expand All @@ -54,7 +54,7 @@ jobs:
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v1
uses: github/codeql-action/autobuild@96f518a34f7a870018057716cc4d7a5c014bd61c # v3.29.10

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl
Expand All @@ -68,4 +68,4 @@ jobs:
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
uses: github/codeql-action/analyze@96f518a34f7a870018057716cc4d7a5c014bd61c # v3.29.10
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ build/
dist/
.cache/
*.egg-info
poetry.lock
.vscode
.python-version
.tox/
14 changes: 0 additions & 14 deletions .travis.yml

This file was deleted.

5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## Unreleased

- Use `inspect.iscoroutinefunction` instead of `asyncio.iscoroutinefunction` to avoid Python 3.14 deprecation warning https://github.com/python-backoff/backoff/pull/3
- Use uv for dependencies and packaging https://github.com/python-backoff/backoff/pull/7

## [v2.2.1] - 2022-10-05

- Fix type hint for wait generators https://github.com/litl/backoff/issues/177
Expand Down
13 changes: 7 additions & 6 deletions backoff/_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
import datetime
import functools
import asyncio
import inspect
from datetime import timedelta

from backoff._common import (_init_wait_gen, _maybe_call, _next_wait)


def _ensure_coroutine(coro_or_func):
if asyncio.iscoroutinefunction(coro_or_func):
if inspect.iscoroutinefunction(coro_or_func):
return coro_or_func
else:
@functools.wraps(coro_or_func)
Expand Down Expand Up @@ -47,10 +48,10 @@ def retry_predicate(target, wait_gen, predicate,
on_giveup = _ensure_coroutines(on_giveup)

# Easy to implement, please report if you need this.
assert not asyncio.iscoroutinefunction(max_tries)
assert not asyncio.iscoroutinefunction(jitter)
assert not inspect.iscoroutinefunction(max_tries)
assert not inspect.iscoroutinefunction(jitter)

assert asyncio.iscoroutinefunction(target)
assert inspect.iscoroutinefunction(target)

@functools.wraps(target)
async def retry(*args, **kwargs):
Expand Down Expand Up @@ -124,8 +125,8 @@ def retry_exception(target, wait_gen, exception,
giveup = _ensure_coroutine(giveup)

# Easy to implement, please report if you need this.
assert not asyncio.iscoroutinefunction(max_tries)
assert not asyncio.iscoroutinefunction(jitter)
assert not inspect.iscoroutinefunction(max_tries)
assert not inspect.iscoroutinefunction(jitter)

@functools.wraps(target)
async def retry(*args, **kwargs):
Expand Down
6 changes: 3 additions & 3 deletions backoff/_decorator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# coding:utf-8
import asyncio
import inspect
import logging
import operator
from typing import Any, Callable, Iterable, Optional, Type, Union
Expand Down Expand Up @@ -98,7 +98,7 @@ def decorate(target):
log_level=giveup_log_level
)

if asyncio.iscoroutinefunction(target):
if inspect.iscoroutinefunction(target):
retry = _async.retry_predicate
else:
retry = _sync.retry_predicate
Expand Down Expand Up @@ -198,7 +198,7 @@ def decorate(target):
log_level=giveup_log_level,
)

if asyncio.iscoroutinefunction(target):
if inspect.iscoroutinefunction(target):
retry = _async.retry_exception
else:
retry = _sync.retry_exception
Expand Down
105 changes: 71 additions & 34 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,44 +1,81 @@
[tool.poetry]
[project]
name = "backoff"
version = "2.2.1"
description = "Function decoration for backoff and retry"
authors = ["Bob Green <[email protected]>"]
authors = [{ name = "Bob Green", email = "[email protected]" }]
requires-python = ">=3.7"
readme = "README.rst"
repository = "https://github.com/litl/backoff"
license = "MIT"
keywords = ["retry", "backoff", "decorators"]
classifiers = ['Development Status :: 5 - Production/Stable',
'Intended Audience :: Developers',
'Programming Language :: Python',
'License :: OSI Approved :: MIT License',
'Natural Language :: English',
'Operating System :: OS Independent',
'Programming Language :: Python',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Topic :: Internet :: WWW/HTTP',
'Topic :: Software Development :: Libraries :: Python Modules',
'Topic :: Utilities']
packages = [
{ include = "backoff" },
keywords = [
"retry",
"backoff",
"decorators",
]
classifiers = [
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
"Programming Language :: Python",
"License :: OSI Approved :: MIT License",
"Natural Language :: English",
"Operating System :: OS Independent",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Programming Language :: Python :: 3.14",
"Topic :: Internet :: WWW/HTTP",
"Topic :: Software Development :: Libraries :: Python Modules",
"Topic :: Utilities",
]

[project.urls]
Repository = "https://github.com/litl/backoff"

[dependency-groups]
dev = [
{ include-group = "tests" },
"flake8>=4.0.1",
"mypy>=0.942",
"types-requests>=2.27.20",
]
tests = [
"pytest>=7.1.2",
"pytest-asyncio>=0.18.3",
"pytest-cov>=3.0.0",
"requests>=2.26.0",
"responses>=0.20.0",
]

[tool.hatch.build.targets.sdist]
include = ["backoff"]

[tool.poetry.dependencies]
python = "^3.7"
[tool.hatch.build.targets.wheel]
include = ["backoff"]

[tool.tox]
min_version = "4.22"
requires = [ "tox", "tox-uv" ]
env_list = [
"3.7",
"3.8",
"3.9",
"3.10",
"3.11",
"3.12",
"3.13",
"3.14",
]

[tool.poetry.dev-dependencies]
flake8 = "^4.0.1"
mypy = "^0.942"
pytest = "^7.1.2"
pytest-asyncio = "^0.18.3"
pytest-cov = "^3.0.0"
requests = "^2.26.0"
responses = "^0.20.0"
types-requests = "^2.27.20"
[tool.tox.env_run_base]
description = "run unit tests"
dependency_groups = [ "tests" ]
commands = [ [ "pytest", { replace = "posargs", default = [ "tests" ], extend = true } ] ]

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
requires = ["hatchling"]
build-backend = "hatchling.build"
53 changes: 2 additions & 51 deletions tests/test_backoff_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ async def exceptor():


@pytest.mark.asyncio
async def test_on_exception_coro_cancelling(event_loop):
async def test_on_exception_coro_cancelling():
sleep_started_event = asyncio.Event()

@backoff.on_predicate(backoff.expo)
Expand All @@ -679,59 +679,10 @@ async def coro():

return False

task = event_loop.create_task(coro())
task = asyncio.create_task(coro())

await sleep_started_event.wait()

task.cancel()

assert (await task)


def test_on_predicate_on_regular_function_without_event_loop(monkeypatch):
monkeypatch.setattr('time.sleep', lambda x: None)

# Set default event loop to None.
loop = asyncio.get_event_loop()
asyncio.set_event_loop(None)

try:
@backoff.on_predicate(backoff.expo)
def return_true(log, n):
val = (len(log) == n - 1)
log.append(val)
return val

log = []
ret = return_true(log, 3)
assert ret is True
assert 3 == len(log)

finally:
# Restore event loop.
asyncio.set_event_loop(loop)


def test_on_exception_on_regular_function_without_event_loop(monkeypatch):
monkeypatch.setattr('time.sleep', lambda x: None)

# Set default event loop to None.
loop = asyncio.get_event_loop()
asyncio.set_event_loop(None)

try:
@backoff.on_exception(backoff.expo, KeyError)
def keyerror_then_true(log, n):
if len(log) == n:
return True
e = KeyError()
log.append(e)
raise e

log = []
assert keyerror_then_true(log, 3) is True
assert 3 == len(log)

finally:
# Restore event loop.
asyncio.set_event_loop(loop)
Loading