{"id":2827,"library":"types-httplib2","title":"Typing Stubs for httplib2","description":"types-httplib2 provides PEP 561 compliant typing stubs for the httplib2 HTTP client library. As part of the typeshed project, its purpose is to enable static type checking for code using httplib2, without altering runtime behavior. The current version is 0.31.2.20260408, with updates released automatically (up to once a day) by typeshed machinery when the underlying library changes or type definitions improve.","status":"active","version":"0.31.2.20260408","language":"en","source_language":"en","source_url":"https://github.com/python/typeshed","tags":["typing","stubs","type hints","httplib2","typeshed"],"install":[{"cmd":"pip install types-httplib2","lang":"bash","label":"Install types-httplib2"}],"dependencies":[{"reason":"Provides type hints for the httplib2 runtime library. Must be installed separately if you want to use httplib2.","package":"httplib2","optional":true}],"imports":[{"note":"Type stubs are automatically picked up by type checkers when importing from the runtime library (httplib2). You do not import directly from 'types-httplib2'.","symbol":"Http","correct":"from httplib2 import Http"},{"note":"Type stubs are automatically picked up by type checkers when importing from the runtime library (httplib2). You do not import directly from 'types-httplib2'.","symbol":"Response","correct":"from httplib2 import Response"}],"quickstart":{"code":"import httplib2\nimport os\n\n# Create an HTTP object, optionally with a cache directory.\n# For a real application, consider a more robust cache path or managing it.\nh = httplib2.Http(os.path.join(os.path.dirname(__file__), \".cache\"))\n\n# Make a simple GET request to a public test API\ntry:\n    resp, content = h.request(\"https://jsonplaceholder.typicode.com/todos/1\", \"GET\")\n\n    # The 'resp' object (type: httplib2.Response) contains headers and status\n    print(f\"Status: {resp.status}\")\n    print(f\"Content-Type: {resp['content-type']}\")\n\n    # The 'content' (type: bytes) is the response body. Decode it if expecting text.\n    if resp.status == 200:\n        data = content.decode('utf-8')\n        print(f\"Content: {data[:100]}...\") # Print first 100 chars\n    else:\n        print(f\"Error fetching data: {content.decode('utf-8')}\")\n\nexcept httplib2.ServerNotFoundError:\n    print(\"Server not found. Check your internet connection or the URL.\")\nexcept Exception as e:\n    print(f\"An unexpected error occurred: {e}\")\n\n# Example of how type checking would apply:\ndef process_response(response: httplib2.Response, body: bytes) -> tuple[int, str]:\n    \"\"\"Processes an httplib2 response and its body.\"\"\"\n    if response.status == 200:\n        # mypy will check if response.status is int and body.decode returns str\n        return response.status, body.decode('utf-8')\n    # mypy will check types of response.status and response.reason\n    return response.status, f\"Error: {response.reason.decode('utf-8')}\"\n\n# To type-check this code:\n# 1. Save the above code as e.g., `my_app.py`\n# 2. Install httplib2 and types-httplib2: `pip install httplib2 types-httplib2`\n# 3. Run mypy from your terminal: `mypy my_app.py`\n#    Mypy will use the stubs from `types-httplib2` to check the types of `resp`, `content`, and parameters/returns in `process_response`.","lang":"python","description":"This quickstart demonstrates a basic HTTP GET request using httplib2. With `types-httplib2` installed, static type checkers like MyPy will automatically use the provided stub files to verify type consistency for httplib2 objects and methods, as shown in the `process_response` function example."},"warnings":[{"fix":"Ensure that `types-httplib2` is updated to a version compatible with your installed `httplib2` version. Typeshed typically aims for stubs to match the latest stable release of the runtime library.","message":"Stub versions are tightly coupled with the runtime package they type. Upgrading `httplib2` to a major new version without also updating `types-httplib2` (or vice-versa) can lead to type-checking errors because the stubs might no longer accurately reflect the underlying library's API.","severity":"breaking","affected_versions":"All versions"},{"fix":"Always use a static type checker (e.g., MyPy, Pyright, Pytype) in your development workflow to leverage the benefits of these stubs.","message":"Type stub packages like `types-httplib2` are solely for static type checking and have no runtime effect. Installing them does not change your program's behavior or prevent runtime errors; they only help type checkers find potential type-related issues before execution.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Monitor `httplib2` release notes and `typeshed` announcements for information regarding inline types. If `httplib2` adds `py.typed`, you might eventually be able to remove `types-httplib2`.","message":"If the `httplib2` library itself starts including inline type annotations (by providing a `py.typed` file), the external stubs from `typeshed` (like `types-httplib2`) may eventually be deprecated or removed. In such cases, type checkers would use the types shipped directly with the `httplib2` package.","severity":"gotcha","affected_versions":"Future versions of httplib2 (if it adopts inline types)"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}