Typing Stubs for requests
raw JSON → 2.33.0.20260327 verified Tue May 12 auth: no python install: verified quickstart: stale
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.
pip install types-requests Common errors
error Cannot find implementation or library stub for module named 'requests' ↓
cause The 'types-requests' package, which provides static type annotations for the 'requests' library, is not installed in the environment where the type checker (e.g., Mypy or Pyright) is being run.
fix
Install the 'types-requests' package using pip:
pip install types-requests error ModuleNotFoundError: No module named 'types_requests' ↓
cause The 'types-requests' package provides stub files for type checkers and is not intended to be imported directly into Python code. The 'requests' library itself should be imported.
fix
Remove any
import types_requests statements. Instead, import requests as usual (e.g., import requests or from requests import get), ensuring that types-requests is installed in your environment for type checking tools. error error: Item "None" of "Optional[Any]" has no attribute "get" ↓
cause This Mypy error occurs when attempting to access keys or methods on the result of `response.json()` without checking if it's `None`. The `json()` method's type annotation correctly indicates it might return `None` if the response body is empty or not valid JSON.
fix
Safely handle the
Optional return type by checking for None before accessing the data, or provide a default value. For example:
import requests
response = requests.get('https://api.example.com/data')
data = response.json()
if data is not None:
value = data.get('key')
print(value)
else:
print('No JSON data or invalid JSON.')
# Or, provide a default empty dict:
safe_data = response.json() or {}
value = safe_data.get('key')
print(value) Warnings
breaking Version incompatibility with `urllib3` prior to types-requests 2.31.0.7. Versions of `types-requests` from `2.31.0.7` onwards require `urllib3>=2`. If your `requests` installation depends on an older `urllib3<2`, installing a newer `types-requests` will cause type checking issues due to conflicting stub dependencies. ↓
fix For projects that *must* use `urllib3<2`, pin `types-requests<2.31.0.7`. For `urllib3>=2` environments, use `types-requests>=2.31.0.7`. Consider aligning your `requests` and `types-requests` version bounds for compatibility.
gotcha Installing `types-requests` does not install `requests` itself. This package only provides type annotations for the `requests` library. The `requests` library must be installed separately for your code to run at runtime. ↓
fix Ensure you have both `pip install requests` and `pip install types-requests` if you want both runtime functionality and type checking in your environment.
gotcha Stub package versioning does not always directly mirror the runtime package version. While the first three parts of `types-requests`'s version generally align with `requests` (e.g., `types-requests==2.33.0.YYYYMMDD` is for `requests~=2.33.0`), the final part is a daily release date from the Typeshed repository. Minor stub updates can introduce new type-checking errors even if the runtime `requests` package has not changed. ↓
fix Pinning `types-requests` to an exact version (e.g., `types-requests==2.33.0.20260327`) in your `requirements.txt` can prevent unexpected type-checking failures, though it means you might miss beneficial stub bug fixes. Alternatively, use version ranges for both `requests` and `types-requests` to manage compatibility.
gotcha Do not attempt to import `types_requests` directly in your Python code. Stub packages are automatically discovered by type checkers and are not meant for direct runtime import. Attempting to import it will result in a `ModuleNotFoundError` during program execution. ↓
fix Always `import requests` in your code. The type checker will correctly associate this import with the installed `types-requests` stubs.
Install
pip install requests types-requests Install compatibility verified last tested: 2026-05-12
python os / libc variant status wheel install import disk
3.10 alpine (musl) requests wheel - 0.52s 21.3M
3.10 alpine (musl) types-requests wheel - - 18.8M
3.10 alpine (musl) requests - - 0.56s 21.2M
3.10 alpine (musl) types-requests - - - -
3.10 slim (glibc) requests wheel 2.1s 0.35s 22M
3.10 slim (glibc) types-requests wheel 1.6s - 19M
3.10 slim (glibc) requests - - 0.40s 22M
3.10 slim (glibc) types-requests - - - -
3.11 alpine (musl) requests wheel - 0.67s 23.3M
3.11 alpine (musl) types-requests wheel - - 20.8M
3.11 alpine (musl) requests - - 0.79s 23.3M
3.11 alpine (musl) types-requests - - - -
3.11 slim (glibc) requests wheel 2.1s 0.58s 24M
3.11 slim (glibc) types-requests wheel 1.7s - 21M
3.11 slim (glibc) requests - - 0.57s 24M
3.11 slim (glibc) types-requests - - - -
3.12 alpine (musl) requests wheel - 0.62s 15.1M
3.12 alpine (musl) types-requests wheel - - 12.7M
3.12 alpine (musl) requests - - 0.67s 15.1M
3.12 alpine (musl) types-requests - - - -
3.12 slim (glibc) requests wheel 1.9s 0.63s 16M
3.12 slim (glibc) types-requests wheel 1.5s - 13M
3.12 slim (glibc) requests - - 0.68s 16M
3.12 slim (glibc) types-requests - - - -
3.13 alpine (musl) requests wheel - 0.60s 14.9M
3.13 alpine (musl) types-requests wheel - - 12.4M
3.13 alpine (musl) requests - - 0.65s 14.7M
3.13 alpine (musl) types-requests - - - -
3.13 slim (glibc) requests wheel 2.0s 0.59s 15M
3.13 slim (glibc) types-requests wheel 1.6s - 13M
3.13 slim (glibc) requests - - 0.64s 15M
3.13 slim (glibc) types-requests - - - -
3.9 alpine (musl) requests wheel - 0.47s 20.5M
3.9 alpine (musl) types-requests wheel - - 18.3M
3.9 alpine (musl) requests - - 0.51s 20.6M
3.9 alpine (musl) types-requests - - - -
3.9 slim (glibc) requests wheel 2.5s 0.42s 21M
3.9 slim (glibc) types-requests wheel 1.9s - 19M
3.9 slim (glibc) requests - - 0.41s 21M
3.9 slim (glibc) types-requests - - - -
Imports
- requests wrong
import types_requestscorrectimport requests - requests.Session
import requests session: requests.Session = requests.Session()
Quickstart stale last tested: 2026-04-23
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.")