Skip to content

Support httpx2 alongside httpx - #334

Open
cig13zs wants to merge 5 commits into
lundberg:masterfrom
cig13zs:httpx2-support
Open

cig13zs wants to merge 5 commits into
lundberg:masterfrom
cig13zs:httpx2-support

Conversation

@cig13zs

@cig13zs cig13zs commented Oct 14, 2026

Copy link
Copy Markdown

Builds on #317 and follows the direction from #316: one respx package that works with either httpx or httpx2.

  • New respx/httpxlib.py, like httpx's jsonlib. Every respx module imports httpx and httpcore from there. It picks httpx if installed, otherwise httpx2. RESPX_HTTPX=httpx or RESPX_HTTPX=httpx2 picks explicitly, e.g. when both are installed. It has to be set before pytest starts, since the plugin imports respx early.
  • The httpx mocker patches the picked client. The default httpcore mocker keeps patching both httpcore and httpcore2, as in Support httpx2 via httpcore2 #317, so requests can't slip through to the network when both are installed.
  • Typing: type checkers assume httpx. Defining RESPX_HTTPX2 as always true (mypy always_true, pyright defineConstant) type-checks against httpx2 instead.
  • Packaging: adds respx[httpx] and respx[httpx2] extras. httpx stays a regular dependency for now, so nothing changes for current installs.
  • Tests run against httpx only, httpx2 only, and both installed (with each pick), via a tool parameter in the noxfile and the CI matrix. Coverage stays at 100% in each, and mypy runs for both.
  • Short docs section under Installation.

I also ran openai-python's test suite with its vendored tests/respx2 pointed at this branch through a small adapter, and no test changed outcome.

Open question: with httpx still required, pip install respx[httpx2] also installs httpx, so httpx2 users need RESPX_HTTPX=httpx2 until httpx becomes optional. Dropping it from install_requires is a one-line change and the httpx2-only env already covers that layout. Happy to do it here or in a follow-up, whichever you prefer.

CI grows from 7 to 17 test jobs. If that's too many I can trim the matrix to fewer Python versions for the httpx2 and both-installed envs.

lundberg and others added 5 commits May 18, 2026 11:50
All respx imports of httpx now go through respx.httpxlib, which uses httpx when installed, else httpx2, or the one set by the RESPX_HTTPX env var. The httpx mocker targets the picked client, and the httpcore mocker responds with the httpcore flavour of the intercepted request.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants