Typing Stubs for requests

2.33.0.20260327 · active · verified Sat Mar 28

types-requests provides static type annotations for the popular `requests` HTTP library. It allows type checkers like Mypy and Pyright to analyze code using `requests` for type correctness. This package, part of the larger Typeshed project, aims to provide accurate annotations for `requests~=2.33.0` and is frequently updated, often daily, from the Typeshed repository.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a basic `requests.get` call with type hints. When `types-requests` is installed, a type checker (like Mypy or Pyright) will use the stub files to validate the types of `url`, `params`, and the return values from `requests.get` and `response.json()` without needing explicit annotations for the `requests` library itself.

import requests
from typing import Dict, Any

def fetch_data(url: str, params: Dict[str, str] = {}) -> Dict[str, Any]:
    """Fetches JSON data from a URL with type hints provided by types-requests."""
    try:
        response = requests.get(url, params=params)
        response.raise_for_status() # Raises HTTPError for bad responses (4xx or 5xx)
        return response.json()
    except requests.exceptions.HTTPError as e:
        print(f"HTTP error occurred: {e}")
        raise
    except requests.exceptions.RequestException as e:
        print(f"An error occurred during the request: {e}")
        raise

# Example usage:
if __name__ == "__main__":
    base_url = "https://httpbin.org/get"
    query_params = {"key1": "value1", "key2": "value2"}
    try:
        data = fetch_data(base_url, query_params)
        print("Fetched data:")
        print(data)
    except Exception:
        print("Failed to fetch data.")

view raw JSON →