Repository navigation
Conversation
|
All CI passing on Python 3.9-3.13, ready for merge |
|
Friendly ping - any chance someone could take a look at this when they get a chance? Happy to make any changes if needed. |
MockTransport now automatically sets response.elapsed to timedelta(0) for both sync and async requests, matching the behavior of real transports and eliminating the need for boilerplate code in handlers. Previously, accessing response.elapsed on MockTransport responses would raise RuntimeError. Now it returns timedelta(0) by default, which is the expected behavior for mocked requests with no actual network I/O. Fixes encode#3712
Add blank lines after docstrings in MockTransport elapsed tests to comply with Ruff formatting rules.
c7216ac to
324e16a
Compare
| response = self.handler(request) | ||
| if not isinstance(response, Response): # pragma: no cover | ||
| raise TypeError("Cannot use an async handler in a sync Client") | ||
| response.elapsed = datetime.timedelta() |
There was a problem hiding this comment.
[P2] Preserve elapsed values explicitly supplied by the mock handler
This unconditionally replaces timing metadata that the handler already supplied, rather than just defaulting a missing value:
def handler(request):
response = httpx.Response(200, text="ok")
response.elapsed = timedelta(seconds=7)
return response
with httpx.Client(transport=httpx.MockTransport(handler)) as client:
assert client.get("http://local.invalid/").elapsed == timedelta(seconds=7)The assertion passes on the base but fails here because elapsed becomes zero. This breaks existing mocks that deliberately simulate response timings, including the explicit assignment shown in #3712. The async assignment has the same regression. Please preserve a supplied elapsed value when adding the default.
Automated review; reproduced through both Client and AsyncClient on Python 3.13.15, base b5addb6 versus head 324e16a.
Summary
Fixes MockTransport to automatically set
response.elapsedtotimedelta(0)for both sync and async requests, eliminating boilerplate and matching real transport behavior.Problem
Previously, accessing
response.elapsedon MockTransport responses would raiseRuntimeError:Users had to add boilerplate in every handler:
Solution
MockTransport now automatically sets
response.elapsed = timedelta(0)after calling the handler, in bothhandle_request()andhandle_async_request().Test Plan
Before fix:
After fix:
Tests:
test_mock_transport_elapsed()for sync clientstest_async_mock_transport_elapsed()for async clientselapsed == timedelta(0)and type correctnessRelated
Fixes #3712