[python] honor Configuration.retries in the httpx client
Fixes #25801
Configuration.retries is documented for the httpx library but ignored by rest.py.
This PR passes it to httpx's own retry mechanism, AsyncHTTPTransport(retries=...),
without working around httpx's limits and without breaking existing setups.
Behavior
| Configuration | Before | After |
|---|---|---|
retries unset, 0 or negative | no retry | unchanged |
retries > 0, direct connection | no retry (bug) | connection failures retried |
retries > 0 + Configuration.proxy | proxy, no retry | unchanged |
retries > 0 + proxy from environment (HTTP(S)_PROXY, ALL_PROXY) | proxy, no retry | unchanged |
httpx retries only ConnectError/ConnectTimeout (the request was never sent), so this
is safe for all HTTP methods, unlike the asyncio client which restricts retries to
ALLOW_RETRY_METHODS.
Why retries are limited to direct connections
With a proxy, httpx's transport retries bring nothing and have a side effect:
- httpx does not retry through a proxy:
AsyncHTTPTransportdoes not forwardretries
toAsyncHTTPProxy/AsyncSOCKSProxy(encode/httpx#2997, open). Checked with httpx
0.28.1 and httpx2 2.16.2:retries=2gives 3 connection attempts directly, 1 through
a proxy. - A custom transport disables proxies from environment variables
(allow_env_proxies = trust_env and transport is None, encode/httpx discussion #2593).
Building it would send requests directly instead of through the proxy.
So the retry transport is only built when there is no proxy: no Configuration.proxy
and none from the environment, detected with urllib.request.getproxies() (what httpx
reads as well; NO_PROXY alone does not count). Otherwise the client is built exactly as
before. This is documented in the Configuration docstring, together with the fact that
each attempt gets the full connect timeout, with httpcore's backoff between attempts
(0 s, 0.5 s, 1 s…).
If encode/httpx#2997 is fixed, the proxy condition can be revisited.
Changes
python/httpx/rest.mustache:_create_pool_manager()builds
AsyncHTTPTransport(retries=..., verify=..., limits=...)whenretries > 0and no
proxy is set.python/configuration.mustache: document whatretriescovers for httpx.- Samples regenerated:
python-httpx,python-httpx-sync,python-httpx2,
python-httpx2-sync. - New
tests/test_retries.pyin these four samples:retries=2against a closed port: 3 real connection attempts, counted at
anyio.connect_tcp;retriesunset,0,-1: 1 attempt, and the client is built as before;retries=3: TLS context and connection limit go to the transport;NO_PROXYalone
keeps the retries;retries=3withConfiguration.proxy, or withHTTP_PROXY,https_proxyor
ALL_PROXYin the environment: the client is built as before.
Testing
- On master, the retry tests fail and the "built as before" tests pass (they pin the
current behavior). - With this change, all of them pass (httpx 0.28.1, httpx2 2.16.2).
- The other failures in these samples are unchanged from master:
test_pet_api.py
needs the petstore server, and mypy reports the same error insigning.py.
PR checklist
- Read the contribution guidelines.
- Built the CLI (
./mvnw clean package -DskipTests -pl modules/openapi-generator-cli -am, template-only change) and ran./bin/generate-samples.shon the fourbin/configs/python-httpx*.yamlconfigs, committed all changed files. No generator option changed, so the generator docs are unaffected. - @mention the Python technical committee: @cbornet @tomplus @krjakbrjak @fa0311 @AndreyVMarkelov
🤖 Generated with Claude Code