Typing stubs for retry
The `types-retry` package provides PEP 561 compatible type stubs for the `retry` library (pypi.org/project/retry), enabling static type checking for code that implements retry logic. As an auto-generated part of the `typeshed` project, it receives regular updates to reflect the API of the `retry` library. The current version is 0.9.9.20260408, with releases typically tied to updates in the upstream `retry` library or `typeshed` itself.
Warnings
- gotcha The `types-retry` package only provides type hints. You must separately install the actual `retry` runtime library (`pip install retry`) for your code to function. Forgetting the runtime library is a common oversight.
- gotcha There are multiple Python libraries offering retry functionality (e.g., `retry`, `retrying`, `tenacity`). `types-retry` specifically provides stubs for the `retry` library (often associated with `github.com/invl/retry`). Ensure you are using the correct runtime library if you are relying on these type stubs.
- gotcha By default, the `@retry` decorator retries indefinitely if no `tries` parameter is specified. This can lead to infinite loops in case of persistent errors, consuming resources or hanging applications.
- breaking As part of `typeshed`, `types-retry` aims to provide accurate annotations for specific versions of the `retry` library (e.g., `retry==0.9.*` for `types-retry==0.9.*`). Major updates to the `retry` library's API might cause type-checking failures if your `types-retry` version is not compatible with your installed `retry` version.
- gotcha Type changes in stub packages can sometimes cause type checkers to fail even if the runtime code remains unchanged. `typeshed` strives to minimize this, but it can occur. Older `mypy` versions might also misinterpret newer `typeshed` features, silently losing type precision.
Install
-
pip install retry -
pip install types-retry
Imports
- retry
from retry import retry
- retry_call
from retry import retry_call
Quickstart
import random
from retry import retry
import os
# Example function that might fail
def unreliable_service_call() -> str:
if random.random() < 0.7: # 70% chance of failure
raise ConnectionError("Failed to connect to service!")
return "Service data retrieved successfully!"
# Decorate the function with retry logic
@retry(exceptions=ConnectionError, tries=3, delay=2000) # Retry 3 times, with 2-second delay
def call_with_retry() -> str:
print("Attempting service call...")
return unreliable_service_call()
if __name__ == "__main__":
try:
result = call_with_retry()
print(result)
except Exception as e:
print(f"All retry attempts failed: {e}")