[python] honor Configuration.retries in the httpx client

B2
2d ago
+551-5

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

ConfigurationBeforeAfter
retries unset, 0 or negativeno retryunchanged
retries > 0, direct connectionno retry (bug)connection failures retried
retries > 0 + Configuration.proxyproxy, no retryunchanged
retries > 0 + proxy from environment (HTTP(S)_PROXY, ALL_PROXY)proxy, no retryunchanged

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:

  1. httpx does not retry through a proxy: AsyncHTTPTransport does not forward retries
    to AsyncHTTPProxy/AsyncSOCKSProxy (encode/httpx#2997, open). Checked with httpx
    0.28.1 and httpx2 2.16.2: retries=2 gives 3 connection attempts directly, 1 through
    a proxy.
  2. 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=...) when retries > 0 and no
    proxy is set.
  • python/configuration.mustache: document what retries covers for httpx.
  • Samples regenerated: python-httpx, python-httpx-sync, python-httpx2,
    python-httpx2-sync.
  • New tests/test_retries.py in these four samples:
    • retries=2 against a closed port: 3 real connection attempts, counted at
      anyio.connect_tcp;
    • retries unset, 0, -1: 1 attempt, and the client is built as before;
    • retries=3: TLS context and connection limit go to the transport; NO_PROXY alone
      keeps the retries;
    • retries=3 with Configuration.proxy, or with HTTP_PROXY, https_proxy or
      ALL_PROXY in 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 in signing.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.sh on the four bin/configs/python-httpx*.yaml configs, 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