Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.10.x changed behavior of retrieving headers with mocks #9297

Closed
1 task done
dblock opened this issue Sep 25, 2024 · 2 comments
Closed
1 task done

3.10.x changed behavior of retrieving headers with mocks #9297

dblock opened this issue Sep 25, 2024 · 2 comments
Labels
bug needs-info Issue is lacking sufficient information and will be closed if not provided

Comments

@dblock
Copy link

dblock commented Sep 25, 2024

Describe the bug

Coming from opensearch-project/opensearch-py#828 in which 2 tests started failing when upgrading from 3.10.5 to 3.10.6 that wasn't expected.

FAILED test_opensearchpy/test_async/test_http_connection.py::TestAsyncHttpConnection::test_basicauth_in_request_session - TypeError: 'coroutine' object is not iterable
FAILED test_opensearchpy/test_async/test_http_connection.py::TestAsyncHttpConnection::test_callable_in_request_session - TypeError: 'coroutine' object is not iterable

The code does response.headers.getall("warning", ()) and before it ended up being the default () and now it's an <coroutine object AsyncMockMixin._execute_mock_call at 0x108757680>.

The mock setup is fairly straightforward.

    @pytest.mark.asyncio  # type: ignore
    @mock.patch("aiohttp.ClientSession.request", new_callable=mock.Mock)
    async def test_basicauth_in_request_session(self, mock_request: Any) -> None:
        async def do_request(*args: Any, **kwargs: Any) -> Any:
            response_mock = mock.AsyncMock()
            response_mock.headers = CIMultiDict()
            response_mock.status = 200
            return response_mock

        mock_request.return_value = aiohttp.client._RequestContextManager(do_request())

I tried isolating the change to a smaller repro but couldn't figure it out.

To Reproduce

  1. Check out https://github.com/opensearch-project/OpenSearch-py.
  2. docker run -d -p 9200:9200 -p 9600:9600 -e OPENSEARCH_INITIAL_ADMIN_PASSWORD=myStrongPassword123! -e "discovery.type=single-node" opensearchproject/opensearch:latest
  3. In dev-requirements.txt set aiohttp==3.10.6.
  4. nox -rs test-3.10

If you use 3.10.5 tests will pass.

Expected behavior

No change between .5 and .6.

Logs/tracebacks

N/A

Python Version

$ python --version

3.10

aiohttp Version

Name: aiohttp
Version: 3.10.6
Summary: Async http client/server framework (asyncio)
Home-page: https://github.com/aio-libs/aiohttp
Author: 
Author-email: 
License: Apache 2
Location: /Users/dblock/.pyenv/versions/3.11.1/lib/python3.11/site-packages
Requires: aiosignal, async-timeout, attrs, charset-normalizer, frozenlist, multidict, yarl
Required-by:

multidict Version

$  python -m pip show multidict

Name: multidict
Version: 6.0.4
Summary: multidict implementation
Home-page: https://github.com/aio-libs/multidict
Author: Andrew Svetlov
Author-email: [email protected]
License: Apache 2
Location: /Users/dblock/.pyenv/versions/3.11.1/lib/python3.11/site-packages
Requires: 
Required-by: aiohttp, yarl

yarl Version

$ python -m pip show yarl
Name: yarl
Version: 1.9.2
Summary: Yet another URL library
Home-page: https://github.com/aio-libs/yarl/
Author: Andrew Svetlov
Author-email: [email protected]
License: Apache-2.0
Location: /Users/dblock/.pyenv/versions/3.11.1/lib/python3.11/site-packages
Requires: idna, multidict
Required-by: aiohttp

OS

MacOS

Related component

Client

Additional context

I apologize for a pretty high level bug report, but hopefully someone who works on this project can easily spot what has changed in 3.10.6 to cause this.

Code of Conduct

  • I agree to follow the aio-libs Code of Conduct
@Dreamsorcerer
Copy link
Member

If you're using async with then the subtle change is that it now returns await self._resp.__aenter__(). If I'm reading correctly, you've just made the resp a Mock, so that method will just return another mock. A real response would return itself.

Ultimately, you should probably be using https://docs.aiohttp.org/en/stable/testing.html#aiohttp.test_utils.make_mocked_request
Which would give you a real Request object with the networking parts mocked out.

@Dreamsorcerer Dreamsorcerer added the needs-info Issue is lacking sufficient information and will be closed if not provided label Sep 26, 2024
@dblock
Copy link
Author

dblock commented Sep 26, 2024

This was very helpful, thank you. Added a mock response class in opensearch-project/opensearch-py#829 that returns self from __aenter__.

@dblock dblock closed this as completed Sep 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug needs-info Issue is lacking sufficient information and will be closed if not provided
Projects
None yet
Development

No branches or pull requests

2 participants