Typing Stubs for Ratelimit

2.2.0.20260408 · active · verified Tue Apr 14

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

Install

Imports

Quickstart

This quickstart demonstrates basic usage of the `ratelimit` library with `types-ratelimit` providing static type checking. The `limits` decorator restricts the number of calls within a period, and `sleep_and_retry` ensures the function waits until the rate limit resets before retrying.

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.")

view raw JSON →