diff --git a/aiohttp/pytest_plugin.py b/aiohttp/pytest_plugin.py index 503bb6a902..8eef2f46d4 100644 --- a/aiohttp/pytest_plugin.py +++ b/aiohttp/pytest_plugin.py @@ -46,7 +46,7 @@ async def __call__( __param: Application, *, server_kwargs: Optional[Dict[str, Any]] = None, - **kwargs: Any + **kwargs: Any, ) -> TestClient[Request, Application]: ... @overload async def __call__( @@ -54,7 +54,7 @@ async def __call__( __param: BaseTestServer[_Request], *, server_kwargs: Optional[Dict[str, Any]] = None, - **kwargs: Any + **kwargs: Any, ) -> TestClient[_Request, None]: ... @@ -70,7 +70,7 @@ def __call__( handler: _RequestHandler[BaseRequest], *, port: Optional[int] = None, - **kwargs: Any + **kwargs: Any, ) -> Awaitable[RawTestServer]: ... @@ -332,7 +332,7 @@ async def go( handler: _RequestHandler[BaseRequest], *, port: Optional[int] = None, - **kwargs: Any + **kwargs: Any, ) -> RawTestServer: server = RawTestServer(handler, port=port) await server.start_server(**kwargs) @@ -392,20 +392,20 @@ async def go( __param: Application, *, server_kwargs: Optional[Dict[str, Any]] = None, - **kwargs: Any + **kwargs: Any, ) -> TestClient[Request, Application]: ... @overload async def go( __param: BaseTestServer[_Request], *, server_kwargs: Optional[Dict[str, Any]] = None, - **kwargs: Any + **kwargs: Any, ) -> TestClient[_Request, None]: ... async def go( __param: Union[Application, BaseTestServer[Any]], *, server_kwargs: Optional[Dict[str, Any]] = None, - **kwargs: Any + **kwargs: Any, ) -> TestClient[Any, Any]: if isinstance(__param, Application): server_kwargs = server_kwargs or {} diff --git a/pyproject.toml b/pyproject.toml index 8b83775c74..e21ba283b1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -87,3 +87,8 @@ ignore-words-list = 'te,assertIn' # TODO(3.13): Remove aiohttp.helpers once https://github.com/python/cpython/pull/106771 # is available in all supported cpython versions exclude-modules = "(^aiohttp\\.helpers)" + +[tool.black] +# TODO: Remove when project metadata is moved here. +# Black can read the value from [project.requires-python]. +target-version = ["py39", "py310", "py311", "py312", "py313"] diff --git a/tests/test_client_functional.py b/tests/test_client_functional.py index 68b647d056..f95ebabaf7 100644 --- a/tests/test_client_functional.py +++ b/tests/test_client_functional.py @@ -2993,9 +2993,10 @@ async def close(self) -> None: connector = aiohttp.TCPConnector(resolver=FakeResolver(), ssl=False) - async with aiohttp.ClientSession(connector=connector) as client, client.get( - url_from, auth=aiohttp.BasicAuth("user", "pass") - ) as resp: + async with ( + aiohttp.ClientSession(connector=connector) as client, + client.get(url_from, auth=aiohttp.BasicAuth("user", "pass")) as resp, + ): assert len(resp.history) == 1 assert str(resp.url) == "http://example.com" assert resp.status == 200 diff --git a/tests/test_client_session.py b/tests/test_client_session.py index 2abf0f1fe3..8ea0ed6c34 100644 --- a/tests/test_client_session.py +++ b/tests/test_client_session.py @@ -574,11 +574,12 @@ async def create_connection( return create_mocked_conn() connector = session._connector - with mock.patch.object(connector, "connect", connect), mock.patch.object( - connector, "_create_connection", create_connection - ), mock.patch.object(connector, "_release"), mock.patch( - "aiohttp.client.os" - ) as m_os: + with ( + mock.patch.object(connector, "connect", connect), + mock.patch.object(connector, "_create_connection", create_connection), + mock.patch.object(connector, "_release"), + mock.patch("aiohttp.client.os") as m_os, + ): m_os.urandom.return_value = key_data await session.ws_connect(f"{protocol}://example") @@ -635,11 +636,12 @@ async def create_connection( return create_mocked_conn() connector = session._connector - with mock.patch.object(connector, "connect", connect), mock.patch.object( - connector, "_create_connection", create_connection - ), mock.patch.object(connector, "_release"), mock.patch( - "aiohttp.client.os" - ) as m_os: + with ( + mock.patch.object(connector, "connect", connect), + mock.patch.object(connector, "_create_connection", create_connection), + mock.patch.object(connector, "_release"), + mock.patch("aiohttp.client.os") as m_os, + ): m_os.urandom.return_value = key_data await session.ws_connect(f"{protocol}://example") diff --git a/tests/test_client_ws.py b/tests/test_client_ws.py index 605e9b58d5..3b1bd00b09 100644 --- a/tests/test_client_ws.py +++ b/tests/test_client_ws.py @@ -60,9 +60,10 @@ async def test_ws_connect_read_timeout_is_reset_to_inf( hdrs.SEC_WEBSOCKET_PROTOCOL: "chat", } resp.connection.protocol.read_timeout = 0.5 - with mock.patch("aiohttp.client.os") as m_os, mock.patch( - "aiohttp.client.ClientSession.request" - ) as m_req: + with ( + mock.patch("aiohttp.client.os") as m_os, + mock.patch("aiohttp.client.ClientSession.request") as m_req, + ): m_os.urandom.return_value = key_data m_req.return_value = loop.create_future() m_req.return_value.set_result(resp) @@ -89,9 +90,10 @@ async def test_ws_connect_read_timeout_stays_inf( hdrs.SEC_WEBSOCKET_PROTOCOL: "chat", } resp.connection.protocol.read_timeout = None - with mock.patch("aiohttp.client.os") as m_os, mock.patch( - "aiohttp.client.ClientSession.request" - ) as m_req: + with ( + mock.patch("aiohttp.client.os") as m_os, + mock.patch("aiohttp.client.ClientSession.request") as m_req, + ): m_os.urandom.return_value = key_data m_req.return_value = loop.create_future() m_req.return_value.set_result(resp) @@ -120,9 +122,10 @@ async def test_ws_connect_read_timeout_reset_to_max( hdrs.SEC_WEBSOCKET_PROTOCOL: "chat", } resp.connection.protocol.read_timeout = 0.5 - with mock.patch("aiohttp.client.os") as m_os, mock.patch( - "aiohttp.client.ClientSession.request" - ) as m_req: + with ( + mock.patch("aiohttp.client.os") as m_os, + mock.patch("aiohttp.client.ClientSession.request") as m_req, + ): m_os.urandom.return_value = key_data m_req.return_value = loop.create_future() m_req.return_value.set_result(resp) @@ -447,9 +450,11 @@ async def test_close_connection_lost( hdrs.SEC_WEBSOCKET_ACCEPT: ws_key, } mresp.connection.protocol.read_timeout = None - with mock.patch("aiohttp.client.WebSocketWriter"), mock.patch( - "aiohttp.client.os" - ) as m_os, mock.patch("aiohttp.client.ClientSession.request") as m_req: + with ( + mock.patch("aiohttp.client.WebSocketWriter"), + mock.patch("aiohttp.client.os") as m_os, + mock.patch("aiohttp.client.ClientSession.request") as m_req, + ): m_os.urandom.return_value = key_data m_req.return_value = loop.create_future() m_req.return_value.set_result(mresp) diff --git a/tests/test_client_ws_functional.py b/tests/test_client_ws_functional.py index d42582f007..42c705ffb9 100644 --- a/tests/test_client_ws_functional.py +++ b/tests/test_client_ws_functional.py @@ -777,11 +777,14 @@ async def handler(request: web.Request) -> NoReturn: # since if we closed the connection normally, the client would # would cancel the heartbeat task and we wouldn't get a ping assert resp._conn is not None - with mock.patch.object( - resp._conn.transport, "write", side_effect=ClientConnectionResetError - ), mock.patch.object( - resp._writer, "send_frame", wraps=resp._writer.send_frame - ) as send_frame: + with ( + mock.patch.object( + resp._conn.transport, "write", side_effect=ClientConnectionResetError + ), + mock.patch.object( + resp._writer, "send_frame", wraps=resp._writer.send_frame + ) as send_frame, + ): await resp.receive() ping_count = send_frame.call_args_list.count(mock.call(b"", WSMsgType.PING)) # Connection should be closed roughly after 1.5x heartbeat. diff --git a/tests/test_connector.py b/tests/test_connector.py index e31fb1dec4..61b452eee8 100644 --- a/tests/test_connector.py +++ b/tests/test_connector.py @@ -814,16 +814,24 @@ def get_extra_info(param: str) -> object: assert False - with mock.patch.object( - conn, "_resolve_host", autospec=True, spec_set=True, side_effect=_resolve_host - ), mock.patch.object( - conn._loop, - "create_connection", - autospec=True, - spec_set=True, - side_effect=create_connection, - ), mock.patch( - "aiohttp.connector.aiohappyeyeballs.start_connection", start_connection + with ( + mock.patch.object( + conn, + "_resolve_host", + autospec=True, + spec_set=True, + side_effect=_resolve_host, + ), + mock.patch.object( + conn._loop, + "create_connection", + autospec=True, + spec_set=True, + side_effect=create_connection, + ), + mock.patch( + "aiohttp.connector.aiohappyeyeballs.start_connection", start_connection + ), ): established_connection = await conn.connect(req, [], ClientTimeout()) @@ -996,16 +1004,24 @@ async def create_connection( pr = create_mocked_conn(loop) return tr, pr - with mock.patch.object( - conn, "_resolve_host", autospec=True, spec_set=True, side_effect=_resolve_host - ), mock.patch.object( - conn._loop, - "create_connection", - autospec=True, - spec_set=True, - side_effect=create_connection, - ), mock.patch( - "aiohttp.connector.aiohappyeyeballs.start_connection", start_connection + with ( + mock.patch.object( + conn, + "_resolve_host", + autospec=True, + spec_set=True, + side_effect=_resolve_host, + ), + mock.patch.object( + conn._loop, + "create_connection", + autospec=True, + spec_set=True, + side_effect=create_connection, + ), + mock.patch( + "aiohttp.connector.aiohappyeyeballs.start_connection", start_connection + ), ): established_connection = await conn.connect(req, [], ClientTimeout()) @@ -1182,16 +1198,24 @@ async def create_connection( pr = create_mocked_conn(loop) return tr, pr - with mock.patch.object( - conn, "_resolve_host", autospec=True, spec_set=True, side_effect=_resolve_host - ), mock.patch.object( - conn._loop, - "create_connection", - autospec=True, - spec_set=True, - side_effect=create_connection, - ), mock.patch( - "aiohttp.connector.aiohappyeyeballs.start_connection", start_connection + with ( + mock.patch.object( + conn, + "_resolve_host", + autospec=True, + spec_set=True, + side_effect=_resolve_host, + ), + mock.patch.object( + conn._loop, + "create_connection", + autospec=True, + spec_set=True, + side_effect=create_connection, + ), + mock.patch( + "aiohttp.connector.aiohappyeyeballs.start_connection", start_connection + ), ): established_connection = await conn.connect(req, [], ClientTimeout()) @@ -1698,8 +1722,11 @@ async def test_exception_during_connetion_create_tracing( assert not conn._acquired assert key not in conn._acquired_per_host - with pytest.raises(asyncio.CancelledError), mock.patch.object( - conn, "_create_connection", autospec=True, spec_set=True, return_value=proto + with ( + pytest.raises(asyncio.CancelledError), + mock.patch.object( + conn, "_create_connection", autospec=True, spec_set=True, return_value=proto + ), ): await conn.connect(req, traces, ClientTimeout()) @@ -1729,8 +1756,11 @@ async def test_exception_during_connection_queued_tracing( assert not conn._acquired assert key not in conn._acquired_per_host - with pytest.raises(asyncio.CancelledError), mock.patch.object( - conn, "_create_connection", autospec=True, spec_set=True, return_value=proto + with ( + pytest.raises(asyncio.CancelledError), + mock.patch.object( + conn, "_create_connection", autospec=True, spec_set=True, return_value=proto + ), ): resp1 = await conn.connect(req, traces, ClientTimeout()) assert resp1 @@ -1767,8 +1797,11 @@ async def test_exception_during_connection_reuse_tracing( assert not conn._acquired assert key not in conn._acquired_per_host - with pytest.raises(asyncio.CancelledError), mock.patch.object( - conn, "_create_connection", autospec=True, spec_set=True, return_value=proto + with ( + pytest.raises(asyncio.CancelledError), + mock.patch.object( + conn, "_create_connection", autospec=True, spec_set=True, return_value=proto + ), ): resp = await conn.connect(req, traces, ClientTimeout()) with mock.patch.object(resp.protocol, "should_close", False): @@ -1987,8 +2020,9 @@ async def test_cleanup_closed( async def test_cleanup_closed_is_noop_on_fixed_cpython() -> None: """Ensure that enable_cleanup_closed is a noop on fixed Python versions.""" - with mock.patch("aiohttp.connector.NEEDS_CLEANUP_CLOSED", False), pytest.warns( - DeprecationWarning, match="cleanup_closed ignored" + with ( + mock.patch("aiohttp.connector.NEEDS_CLEANUP_CLOSED", False), + pytest.warns(DeprecationWarning, match="cleanup_closed ignored"), ): conn = aiohttp.BaseConnector(enable_cleanup_closed=True) assert conn._cleanup_closed_disabled is True @@ -2257,9 +2291,12 @@ async def delay_resolve(*args: object, **kwargs: object) -> List[ResolveResult]: req = ClientRequest( "GET", URL("http://localhost:80"), loop=loop, response_class=mock.Mock() ) - with mock.patch.object(conn._resolver, "resolve", delay_resolve), mock.patch( - "aiohttp.connector.aiohappyeyeballs.start_connection", - side_effect=OSError(1, "Forced connection to fail"), + with ( + mock.patch.object(conn._resolver, "resolve", delay_resolve), + mock.patch( + "aiohttp.connector.aiohappyeyeballs.start_connection", + side_effect=OSError(1, "Forced connection to fail"), + ), ): task1 = asyncio.create_task(conn.connect(req, [], ClientTimeout())) @@ -2307,9 +2344,12 @@ async def delay_resolve(*args: object, **kwargs: object) -> List[ResolveResult]: req = ClientRequest( "GET", URL("http://localhost:80"), loop=loop, response_class=mock.Mock() ) - with mock.patch.object(conn._resolver, "resolve", delay_resolve), mock.patch( - "aiohttp.connector.aiohappyeyeballs.start_connection", - side_effect=OSError(1, "Forced connection to fail"), + with ( + mock.patch.object(conn._resolver, "resolve", delay_resolve), + mock.patch( + "aiohttp.connector.aiohappyeyeballs.start_connection", + side_effect=OSError(1, "Forced connection to fail"), + ), ): task1 = asyncio.create_task(conn.connect(req, [], ClientTimeout())) @@ -2357,9 +2397,12 @@ async def delay_resolve(*args: object, **kwargs: object) -> List[ResolveResult]: req = ClientRequest( "GET", URL("http://localhost:80"), loop=loop, response_class=mock.Mock() ) - with mock.patch.object(conn._resolver, "resolve", delay_resolve), mock.patch( - "aiohttp.connector.aiohappyeyeballs.start_connection", - side_effect=OSError(1, "Forced connection to fail"), + with ( + mock.patch.object(conn._resolver, "resolve", delay_resolve), + mock.patch( + "aiohttp.connector.aiohappyeyeballs.start_connection", + side_effect=OSError(1, "Forced connection to fail"), + ), ): task1 = asyncio.create_task(conn.connect(req, [], ClientTimeout())) @@ -2415,9 +2458,12 @@ async def delay_resolve(*args: object, **kwargs: object) -> List[ResolveResult]: req = ClientRequest( "GET", URL("http://localhost:80"), loop=loop, response_class=mock.Mock() ) - with mock.patch.object(conn._resolver, "resolve", delay_resolve), mock.patch( - "aiohttp.connector.aiohappyeyeballs.start_connection", - side_effect=OSError(1, "Forced connection to fail"), + with ( + mock.patch.object(conn._resolver, "resolve", delay_resolve), + mock.patch( + "aiohttp.connector.aiohappyeyeballs.start_connection", + side_effect=OSError(1, "Forced connection to fail"), + ), ): task1 = asyncio.create_task(conn.connect(req, [], ClientTimeout())) @@ -2480,9 +2526,12 @@ async def delay_resolve(*args: object, **kwargs: object) -> List[ResolveResult]: req = ClientRequest( "GET", URL("http://localhost:80"), loop=loop, response_class=mock.Mock() ) - with mock.patch.object(conn._resolver, "resolve", delay_resolve), mock.patch( - "aiohttp.connector.aiohappyeyeballs.start_connection", - side_effect=OSError(1, "Forced connection to fail"), + with ( + mock.patch.object(conn._resolver, "resolve", delay_resolve), + mock.patch( + "aiohttp.connector.aiohappyeyeballs.start_connection", + side_effect=OSError(1, "Forced connection to fail"), + ), ): task1 = asyncio.create_task(conn.connect(req, [], ClientTimeout())) diff --git a/tests/test_http_writer.py b/tests/test_http_writer.py index f6828e1938..69e89bb34c 100644 --- a/tests/test_http_writer.py +++ b/tests/test_http_writer.py @@ -279,9 +279,12 @@ async def test_write_payload_deflate_compression_chunked_connection_lost( msg.enable_compression("deflate") msg.enable_chunking() await msg.write(b"data") - with pytest.raises( - ClientConnectionResetError, match="Cannot write to closing transport" - ), mock.patch.object(transport, "is_closing", return_value=True): + with ( + pytest.raises( + ClientConnectionResetError, match="Cannot write to closing transport" + ), + mock.patch.object(transport, "is_closing", return_value=True), + ): await msg.write_eof(b"end") diff --git a/tests/test_proxy.py b/tests/test_proxy.py index ec99079236..2aaafedeb5 100644 --- a/tests/test_proxy.py +++ b/tests/test_proxy.py @@ -455,18 +455,21 @@ def close(self) -> None: fingerprint_mock.check.side_effect = aiohttp.ServerFingerprintMismatch( b"exp", b"got", "example.com", 8080 ) - with mock.patch.object( - proxy_req, - "send", - autospec=True, - spec_set=True, - return_value=proxy_resp, - ), mock.patch.object( - proxy_resp, - "start", - autospec=True, - spec_set=True, - return_value=mock.Mock(status=200), + with ( + mock.patch.object( + proxy_req, + "send", + autospec=True, + spec_set=True, + return_value=proxy_resp, + ), + mock.patch.object( + proxy_resp, + "start", + autospec=True, + spec_set=True, + return_value=mock.Mock(status=200), + ), ): connector = self.loop.run_until_complete(make_conn()) host = [ @@ -479,30 +482,35 @@ def close(self) -> None: "flags": 0, } ] - with mock.patch.object( - connector, - "_resolve_host", - autospec=True, - spec_set=True, - return_value=host, - ), mock.patch.object( - connector, - "_get_fingerprint", - autospec=True, - spec_set=True, - return_value=fingerprint_mock, - ), mock.patch.object( # Called on connection to http://proxy.example.com - self.loop, - "create_connection", - autospec=True, - spec_set=True, - return_value=(mock.Mock(), mock.Mock()), - ), mock.patch.object( # Called on connection to https://www.python.org - self.loop, - "start_tls", - autospec=True, - spec_set=True, - return_value=TransportMock(), + with ( + mock.patch.object( + connector, + "_resolve_host", + autospec=True, + spec_set=True, + return_value=host, + ), + mock.patch.object( + connector, + "_get_fingerprint", + autospec=True, + spec_set=True, + return_value=fingerprint_mock, + ), + mock.patch.object( # Called on connection to http://proxy.example.com + self.loop, + "create_connection", + autospec=True, + spec_set=True, + return_value=(mock.Mock(), mock.Mock()), + ), + mock.patch.object( # Called on connection to https://www.python.org + self.loop, + "start_tls", + autospec=True, + spec_set=True, + return_value=TransportMock(), + ), ): req = ClientRequest( "GET", diff --git a/tests/test_proxy_functional.py b/tests/test_proxy_functional.py index a7aa2ce6f5..3f2e4a26b4 100644 --- a/tests/test_proxy_functional.py +++ b/tests/test_proxy_functional.py @@ -218,13 +218,16 @@ async def test_https_proxy_unsupported_tls_in_tls( r"$" ) - with pytest.warns( - RuntimeWarning, - match=expected_warning_text, - ), pytest.raises( - ClientConnectionError, - match=expected_exception_reason, - ) as conn_err: + with ( + pytest.warns( + RuntimeWarning, + match=expected_warning_text, + ), + pytest.raises( + ClientConnectionError, + match=expected_exception_reason, + ) as conn_err, + ): async with sess.get(url, proxy=secure_proxy_url, ssl=client_ssl_ctx): pass diff --git a/tests/test_web_response.py b/tests/test_web_response.py index 545a6f7ac5..937dfd8776 100644 --- a/tests/test_web_response.py +++ b/tests/test_web_response.py @@ -437,9 +437,10 @@ async def test_force_compression_deflate_large_payload() -> None: resp.enable_compression(web.ContentCoding.deflate) assert resp.compression - with pytest.warns( - Warning, match="Synchronous compression of large response bodies" - ), mock.patch("aiohttp.web_response.LARGE_BODY_SIZE", 2): + with ( + pytest.warns(Warning, match="Synchronous compression of large response bodies"), + mock.patch("aiohttp.web_response.LARGE_BODY_SIZE", 2), + ): msg = await resp.prepare(req) assert msg is not None assert "deflate" == resp.headers.get(hdrs.CONTENT_ENCODING) diff --git a/tests/test_web_websocket_functional.py b/tests/test_web_websocket_functional.py index 1bb4760b71..43e98e9f45 100644 --- a/tests/test_web_websocket_functional.py +++ b/tests/test_web_websocket_functional.py @@ -782,11 +782,14 @@ async def handler(request: web.Request) -> NoReturn: # would cancel the heartbeat task and we wouldn't get a ping assert ws_server._req is not None assert ws_server._writer is not None - with mock.patch.object( - ws_server._req.transport, "write", side_effect=ConnectionResetError - ), mock.patch.object( - ws_server._writer, "send_frame", wraps=ws_server._writer.send_frame - ) as send_frame: + with ( + mock.patch.object( + ws_server._req.transport, "write", side_effect=ConnectionResetError + ), + mock.patch.object( + ws_server._writer, "send_frame", wraps=ws_server._writer.send_frame + ) as send_frame, + ): try: await ws_server.receive() finally: