Fix: responses/__init__.py's _on_request() (~line 1099) mutated the SAME... - #813
Open
M001N wants to merge 1 commit into
Open
Fix: responses/__init__.py's _on_request() (~line 1099) mutated the SAME...#813M001N wants to merge 1 commit into
M001N wants to merge 1 commit into
Conversation
markstory
reviewed
Aug 21, 2026
| constructed_url = r"http://example.com/test?I+am=a+big+test&hello=world" | ||
| assert resp.url == constructed_url | ||
| assert resp.request.url == constructed_url | ||
| assert resp.request.params == params |
Member
There was a problem hiding this comment.
This could break userland code. But I don't see how we fix the reported issue without removing this attribute.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
In _on_request(), params/req_kwargs are still set on
requestbefore calling _find_match() (needed because matchers.py's query_param_matcher/request_kwargs_matcher read them during matching). Immediately after matching completes, a shallow copy (call_request = copy.copy(request)) is taken to carry those attributes for the internal call log, anddel request.params/del request.req_kwargsstrips them from the live request object before it can flow onward to the caller (via response.request or a raised exception's .request). All Call(...)/self._calls.add(...) construction sites in _on_request were switched to usecall_requestinstead ofrequest, soresponses.calls[i].request.params/.req_kwargskeep working exactly as documented. Updated README.rst's query-params example and two existing tests (test_matchers.py::test_request_matches_params and test_responses.py's assert_params helper used by test_request_param*) that had been asserting .params on the live resp.request object, to instead read it from responses.calls[...].request, since that behavior was the actual bug.Problem
getsentry/responses issue reference: #738
Root Cause
responses/init.py's _on_request() (~line 1099) mutated the SAME live PreparedRequest instance that requests threads back to the caller (via response.request / exception.request) by doing request.params = ... and request.req_kwargs = kwargs directly on it. Since that mutation happened before matching and was never undone, these responses-internal attributes stayed attached to the request object visible to caller code (e.g. in a raised exception's .request), leaking mock-only internals into what should look like a plain, real PreparedRequest.
Testing
PASS - full suite: 231 passed, 0 failed (18 pre-existing unrelated deprecation warnings).
Related Issue
#738