Transform ID:Documentation Index
Fetch the complete documentation index at: https://docs.refactron.dev/llms.txt
Use this file to discover all available pages before exploring further.
deprecated_api_requests_to_httpx
Language: Python
What it does
Rewritesrequests imports and call sites to the API-compatible httpx equivalent. The mapping is hardcoded — requests and httpx share the same get/post/put/delete/head/patch/options surface and identical kwargs (url, timeout, json, headers, etc.).
Detector pattern
The detector atsrc/analyze/detectors/python/deprecated-api.ts looks for:
import requestsfrom requests import <name>requests.<method>(...)call expressions
Preconditions
httpxis not already imported in the file (avoid double-imports / shadowing).- The file only touches the safe httpx drop-in surface — the HTTP verb helpers
get/post/put/delete/patch/head/options/request. If it references any otherrequestsAPI — exception classes likeTimeoutorConnectionError,Session, therequests.exceptionssubmodule — the transform refuses the whole file via theunsafe-api-surfaceprecondition.httpxdiverges there (httpx.Timeoutis a config class, not an exception; there is nohttpx.ConnectionError— it isConnectError), so a blind rename would emit code that breaks at runtime. - Cross-file: no external file in the project references
<this_module>.requestsin any context. The check covers:import this_module; print(this_module.requests)(attribute reads).from unittest.mock import patch; patch("this_module.requests.get", ...)(string-target mock patches — these would silently break post-rename).
Before / after
Edge cases handled
- Both
import requestsandfrom requests import getforms. - All HTTP-method call sites (
requests.get,.post,.put,.delete, etc.). - Cross-file string-target mocks (
patch("module.requests.get", ...)).
Edge cases NOT handled (skip via precondition)
- File already imports
httpx(manual migration probably in progress). - An external file in the project patches or references
<module>.requests. - Files using
requestsAPI outside the safe verb set — exception classes (Timeout,ConnectionError,HTTPError),Session,requests.exceptions. Blocked by theunsafe-api-surfaceprecondition rather than rewritten into runtime-broken code —httpx’s exception and client surface is not a drop-in forrequests.
After the transform applies, you must add
httpx to your dependencies (pip install httpx or pyproject.toml). Refactron does not modify your dependency manifest.