responses: TypeError: cannot unpack non-iterable CallbackResponse object

After upgrading from responses 0.12.0 to 0.12.1 our unit tests fail with the following error:

...

../../../../venv/lib/python3.8/site-packages/requests/sessions.py:590: in post
    return self.request('POST', url, data=data, json=json, **kwargs)
../../../../venv/lib/python3.8/site-packages/requests_oauthlib/oauth2_session.py:515: in request
    return super(OAuth2Session, self).request(
../../../../venv/lib/python3.8/site-packages/aws_xray_sdk/ext/requests/patch.py:27: in _xray_traced_requests
    return xray_recorder.record_subsegment(
../../../../venv/lib/python3.8/site-packages/aws_xray_sdk/core/recorder.py:424: in record_subsegment
    return_value = wrapped(*args, **kwargs)
../../../../venv/lib/python3.8/site-packages/requests/sessions.py:542: in request
    resp = self.send(prep, **send_kwargs)
../../../../venv/lib/python3.8/site-packages/requests/sessions.py:655: in send
    r = adapter.send(request, **kwargs)
../../../../venv/lib/python3.8/site-packages/responses.py:733: in unbound_on_send
    return self._on_request(adapter, request, *a, **kwargs)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <responses.RequestsMock object at 0x104e06880>
adapter = <requests.adapters.HTTPAdapter object at 0x10a7a8a00>
request = <PreparedRequest [POST]>
kwargs = {'cert': None, 'proxies': OrderedDict(), 'stream': False, 'timeout': 10, ...}

    def _on_request(self, adapter, request, **kwargs):
>       match, match_failed_reasons = self._find_match(request)
E       TypeError: cannot unpack non-iterable CallbackResponse object

../../../../venv/lib/python3.8/site-packages/responses.py:680: TypeError

This seems to have been caused by the refactoring of the Response._find_match() method.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 8
  • Comments: 19 (11 by maintainers)

Commits related to this issue

Most upvoted comments

@hramezani I’ve also tried to create a minimal example for you. It passes on my machine when I use responses version 0.12.0 and fails when I upgrade to 0.12.1:

# coding=utf-8
import responses
from requests_oauthlib import OAuth2Session


@responses.activate
def test_response():
    # given
    responses.add(
        method = responses.POST,
        url = "https://example.com/",
        body = "expected",
        status = 200
    )

    # when
    oauth_session = OAuth2Session()
    response = oauth_session.post(
        url = "https://example.com/",
        data = "actual"
    )

    # then
    assert len(responses.calls) == 1
    assert responses.calls[0].request.url == "https://example.com/"
    assert responses.calls[0].request.body == "actual"
    assert response.text == "expected"

I created a PR(https://github.com/getsentry/responses/pull/359) to address this problem.

We have the same issue with a slightly different error message.

self = <responses.RequestsMock object at 0x7f1cb1ece7f0>
adapter = <requests.adapters.HTTPAdapter object at 0x7f1cba8af2b0>
request = <PreparedRequest [GET]>
kwargs = {'cert': None, 'proxies': OrderedDict(), 'stream': False, 'timeout': None, ...}
    def _on_request(self, adapter, request, **kwargs):
>       match, match_failed_reasons = self._find_match(request)
E       TypeError: 'Response' object is not iterable
/usr/local/lib/python3.6/site-packages/responses.py:680: TypeError

Why is moto mocking internals of responses?

We’re skipping the 0.12.1 version for now: responses>=0.9.0,!=0.12.1 in the requirements.

Hi @hramezani Here is a simplified Version of the test that is failing for us.

    @responses.activate
    def test_error(self, mocker):
        mocker.patch("mock_a_function", return_value="test")

        stage = "dev"
        responses.add(
            responses.GET,
            "https://someInternalApi",
            json={
                "items": [
                    {"id1": 123, "id2": 123},
                    {"id1": 123, "id2": 456},
                ],
                "last": True,
            },
            status=200,
        )
        # test id 123
        query_result_1 = query_api(
            id="123", stage=stage
        )


    def query_api(id, stage):
        r = requests.get(
            "https://someInternalApi{stage}/{id}",,
            auth=(username, password),
            params={"page": page, "size": 10},
        )
        return r


Further I found out that our test works with moto==1.3.14 but fails with moto=1.3.16.