Coverage for /home/runner/work/bijux-cli/bijux-cli/src/bijux_cli/contracts/retry.py: 100%
8 statements
« prev ^ index » next coverage.py v7.10.4, created at 2025-08-19 23:36 +0000
« prev ^ index » next coverage.py v7.10.4, created at 2025-08-19 23:36 +0000
1# SPDX-License-Identifier: MIT
2# Copyright © 2025 Bijan Mousavi
4"""Defines the contract for operation retry policies.
6This module specifies the `RetryPolicyProtocol`, a formal interface that any
7service providing retry logic for potentially failing operations must
8implement. This is particularly useful for handling transient errors in
9network requests or other I/O-bound tasks.
10"""
12from __future__ import annotations
14from collections.abc import Awaitable, Callable
15from typing import Protocol, TypeVar, runtime_checkable
17T = TypeVar("T")
20@runtime_checkable
21class RetryPolicyProtocol(Protocol):
22 """Defines the contract for retry policies in asynchronous operations.
24 This interface specifies the methods for executing an operation with retry
25 logic (e.g., exponential backoff) and for resetting the policy's internal
26 state.
27 """
29 async def run(
30 self, supplier: Callable[[], Awaitable[T]], seconds: float = 1.0
31 ) -> T:
32 """Runs an asynchronous operation with a retry policy.
34 Implementations of this method will repeatedly call the `supplier`
35 until it succeeds or the retry policy is exhausted.
37 Args:
38 supplier (Callable[[], Awaitable[T]]): A no-argument function that
39 returns an awaitable (e.g., a coroutine).
40 seconds (float): The timeout for each attempt in seconds.
42 Returns:
43 T: The successful result of the operation.
44 """
45 ...
47 def reset(self) -> None:
48 """Resets the internal state of the retry policy.
50 This is useful for reusing a policy instance for a new, independent
51 operation.
52 """
53 ...