{"id":1886,"library":"types-retry","title":"Typing stubs for retry","description":"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.","status":"active","version":"0.9.9.20260408","language":"en","source_language":"en","source_url":"https://github.com/python/typeshed","tags":["typing","stubs","type-hints","retry","typeshed"],"install":[{"cmd":"pip install retry","lang":"bash","label":"Install the runtime library"},{"cmd":"pip install types-retry","lang":"bash","label":"Install the typing stubs"}],"dependencies":[{"reason":"Provides type hints for the runtime 'retry' library.","package":"retry","optional":false}],"imports":[{"note":"This is the primary decorator for adding retry logic to functions.","symbol":"retry","correct":"from retry import retry"},{"note":"Used to call a function with retry logic directly, without a decorator.","symbol":"retry_call","correct":"from retry import retry_call"}],"quickstart":{"code":"import random\nfrom retry import retry\nimport os\n\n# Example function that might fail\ndef unreliable_service_call() -> str:\n    if random.random() < 0.7:  # 70% chance of failure\n        raise ConnectionError(\"Failed to connect to service!\")\n    return \"Service data retrieved successfully!\"\n\n# Decorate the function with retry logic\n@retry(exceptions=ConnectionError, tries=3, delay=2000) # Retry 3 times, with 2-second delay\ndef call_with_retry() -> str:\n    print(\"Attempting service call...\")\n    return unreliable_service_call()\n\nif __name__ == \"__main__\":\n    try:\n        result = call_with_retry()\n        print(result)\n    except Exception as e:\n        print(f\"All retry attempts failed: {e}\")","lang":"python","description":"This quickstart demonstrates how to apply the `@retry` decorator to an unreliable function. It configures the decorator to retry specifically on `ConnectionError` exceptions, up to 3 times, with a 2-second delay between attempts. Without `types-retry`, type checkers would have limited understanding of the decorator's effects on the function signature; with it, they can provide more accurate analysis."},"warnings":[{"fix":"Ensure both `pip install retry` and `pip install types-retry` are executed in your environment.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Verify that your `import` statements and usage align with the `retry` library, not `retrying` or `tenacity`.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always set a `tries` limit (e.g., `tries=3`) or a `stop` condition (e.g., `stop_max_delay`) to prevent infinite retries.","message":"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.","severity":"gotcha","affected_versions":"All versions of `retry` (runtime library)"},{"fix":"Pin your `types-retry` version to match the expected `retry` library version, or explicitly manage compatibility. For example, if you use `retry==0.9.x`, use `types-retry>=0.9.0,<1.0.0` or `types-retry==0.9.9.20260408` (specific release of stubs).","message":"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.","severity":"breaking","affected_versions":"All versions"},{"fix":"Consider pinning specific versions of `types-retry` and your type checker (e.g., `mypy`) in your development environment to ensure consistent type checking results.","message":"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.","severity":"gotcha","affected_versions":"All versions, especially with older type checkers"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}