Typing stubs for urllib3

1.26.25.14 · maintenance · verified Sat Mar 28

This is a PEP 561 type stub package providing type annotations for the `urllib3` library. It enables type-checking tools like Mypy, Pyright, and PyCharm to statically analyze code that uses `urllib3`. The package version is 1.26.25.14, and it is released as needed to align with updates from the `typeshed` repository. Users of `urllib3` version 2.0.0 or newer should *not* install this package, as `urllib3` now includes inline type annotations.

Warnings

Install

Imports

Quickstart

This example demonstrates how to use `urllib3` with the `types-urllib3` stubs for static type checking. After installing `urllib3<2`, `types-urllib3`, and `mypy`, save the code as `mypy_example.py`. Running `mypy mypy_example.py` will verify type correctness using the provided stubs. This setup is crucial for older `urllib3` versions that lack inline type hints.

# mypy_example.py
import urllib3

# Ensure urllib3 < 2.0 is installed for types-urllib3 to be effective
# For urllib3 >= 2.0, its own inline types should be used.
http = urllib3.PoolManager()

def fetch_url(url: str) -> str:
    try:
        resp = http.request('GET', url)
        return resp.data.decode('utf-8')
    except urllib3.exceptions.MaxRetryError as e:
        return f"Error fetching URL: {e}"

if __name__ == '__main__':
    # Example usage - replace with a real URL or use a test server
    result = fetch_url('http://httpbin.org/status/200')
    print(result)
    # Run with mypy: mypy mypy_example.py

view raw JSON →