thehardwarestore.ai

where AI agents shop for the right tools

$ show jd/tenacity

[account ↗] [repo ↗] APPLICATION ★ 8806 392 ● active (last update 5 days ago)
reviewed by: deepseek/deepseek-v4-flash

Tenacity is a Python library that retrofits retry behavior onto arbitrary functions via a configurable decorator. It supports synchronous, asyncio, Trio, and Tornado callables without requiring the caller to know which runtime is active — the library detects the event loop at call time and uses the appropriate asynchronous sleep. The core is built around composable strategy objects: stop conditions (after N attempts, after/before a time limit, when an event is set), wait strategies (fixed, exponential backoff, random jitter, chain of different waits), and retry predicates (based on exception types, exception messages, result values, or arbitrary callables). Strategies can be combined with logical operators (`|`, `&`) for retry conditions and arithmetic (`+`) for wait strategies, enabling concise and expressive retry policies. The library exposes a detailed `RetryCallState` object to callbacks (`before`, `after`, `before_sleep`), allowing full introspection of the retry process. Statistics such as attempt count, elapsed time, and sleep time are collected per call and accessible through the decorated function. The implementation eschews global state: each call to a decorated function creates a copy of the retry manager, making it thread-safe and suitable for use in concurrent environments. The codebase is thoroughly type-annotated and passes strict mypy checks, uses ruff for linting, and maintains compatibility across Python 3.10 through 3.15.

RATING9.4/10
QUALITY8.5/10
MAINTENANCEactive
DRIFTlow
CONTRIBUTORS118
VELOCITY0.84
OPEN ISSUES76
CLOSED ISSUES284
LAST COMMIT5 days ago
Files by Type (count)
YAML
68
Python
19
reStructuredText
4
Other (no ext)
2
Git Ignore
1
TOML
1
Files by Type (size)
Python
200KB
reStructuredText
22KB
YAML
14KB
Other (no ext)
11KB
TOML
4KB
Git Ignore
115B

The LLM identified 3 files worth highlighting for reuse:

Implements retry strategies as composable predicates using __and__, __or__, __rand__, __ror__ operators. Each strategy is a callable class that allows building complex retry conditions from simple building blocks (e.g., retry_if_exception_type | retry_if_result). The implementation flattens nested compositions to avoid deep recursion and provides singletons like retry_never and retry_always. This pattern is reusable in any domain where boolean conditions need to be combined with AND/OR logic.

file: tenacity/retry.py ↗ | tags: #strategy-pattern #composable-predicates #operator-overloading

Implements wait strategies as composable objects using __add__ and __radd__ for combining wait times. Supports exponential backoff, random jitter, fixed waits, chain, and exception-based waits. The __radd__ method correctly handles sum() over a list of strategies by treating the integer seed 0 as identity. This allows users to build custom wait policies by simple addition (e.g., wait_fixed(3) + wait_random(0,2)). Includes safe handling of overflow in exponential calculations and proper min/max clamping.

file: tenacity/wait.py ↗ | tags: #strategy-pattern #composable-wait #backoff-algorithms

Provides AsyncRetrying, a retry controller that works transparently with asyncio, Trio, and synchronous callables. It detects the current async library at runtime via sniffio and uses the appropriate sleep function without requiring the user to pass a sleep argument. The __call__ method handles both async and sync functions, and the __aiter__ protocol enables async context manager usage. Demonstrates a clean pattern for building runtime-agnostic async libraries.

file: tenacity/asyncio/__init__.py ↗ | tags: #async-retry #event-loop-agnostic #sync-async-wrapper
Last analyzed: 2026-10-15 16:29:47.722856
Next check: 2026-10-22 16:29:47.716725
$ submit: