Typing Stubs for Ratelimit
types-ratelimit provides PEP 561 type stubs for the `ratelimit` library, enabling static type checking for projects using `tomasbasham/ratelimit`. It helps identify type-related issues before runtime by providing annotations for functions, classes, and methods. As part of the typeshed project, its releases are aligned with typeshed's update cadence and aim to match specific versions of the `ratelimit` library.
Warnings
- breaking Type stubs from typeshed, including `types-ratelimit`, may introduce breaking changes to type checking even in minor updates, as they reflect evolving upstream libraries or typeshed's own typing improvements.
- gotcha The `types-ratelimit` package provides type hints for the `ratelimit` library (specifically `tomasbasham/ratelimit`). If you are using a different rate-limiting library (e.g., `upstash-ratelimit` or `pyrate-limiter`), these stubs will not be applicable and may cause incorrect type-checking results or errors.
- gotcha Typeshed stubs, including `types-ratelimit`, are typically maintained to be compatible with a specific range of the runtime library's versions (e.g., `ratelimit==2.2.*`). Discrepancies between the stub version and your installed `ratelimit` version can lead to incorrect type checking or unannotated code.
Install
-
pip install types-ratelimit
Imports
- limits
from ratelimit import limits
- sleep_and_retry
from ratelimit import sleep_and_retry
Quickstart
import time
from ratelimit import limits, sleep_and_retry
# Define a period for rate limiting (e.g., 60 seconds)
ONE_MINUTE = 60
@sleep_and_retry
@limits(calls=5, period=ONE_MINUTE)
def call_api(attempt: int) -> str:
"""Simulates an API call with rate limiting."""
print(f"Calling API - Attempt {attempt}")
# Simulate some work
# In a real scenario, this would make an external request
return f"API response for attempt {attempt}"
print("Starting API calls...")
for i in range(1, 10):
try:
result = call_api(i)
print(result)
except Exception as e:
print(f"Error: {e}")
# Small delay to make output clearer, not part of ratelimit logic
time.sleep(1)
print("Finished API calls.")