From 8743741b03e4bbfa237ee939af1e21a9c0658905 Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Fri, 2 Nov 2018 20:59:33 +0200 Subject: [PATCH] Deprecate .loop property (#3374) --- CHANGES/3374.removal | 1 + aiohttp/client.py | 3 +++ aiohttp/connector.py | 3 +++ aiohttp/web_app.py | 5 ++++- aiohttp/web_fileresponse.py | 2 +- aiohttp/web_request.py | 3 +++ aiohttp/web_ws.py | 2 +- tests/test_client_connection.py | 3 ++- tests/test_client_session.py | 3 ++- tests/test_loop.py | 4 ++-- tests/test_web_app.py | 15 ++++++++++----- tests/test_web_request.py | 8 ++++++++ 12 files changed, 40 insertions(+), 12 deletions(-) create mode 100644 CHANGES/3374.removal diff --git a/CHANGES/3374.removal b/CHANGES/3374.removal new file mode 100644 index 00000000000..45c745944bb --- /dev/null +++ b/CHANGES/3374.removal @@ -0,0 +1 @@ +Deprecate ``app.loop``, ``request.loop``, ``client.loop`` and ``connector.loop`` properties. diff --git a/aiohttp/client.py b/aiohttp/client.py index 7784c9ea675..714bbcd98d5 100644 --- a/aiohttp/client.py +++ b/aiohttp/client.py @@ -836,6 +836,9 @@ def version(self) -> Tuple[int, int]: @property def loop(self) -> asyncio.AbstractEventLoop: """Session's loop.""" + warnings.warn("client.loop property is deprecated", + DeprecationWarning, + stacklevel=2) return self._loop def detach(self) -> None: diff --git a/aiohttp/connector.py b/aiohttp/connector.py index 4277d8aa44e..a69fcab0a80 100644 --- a/aiohttp/connector.py +++ b/aiohttp/connector.py @@ -94,6 +94,9 @@ def __del__(self, _warnings: Any=warnings) -> None: @property def loop(self) -> asyncio.AbstractEventLoop: + warnings.warn("connector.loop property is deprecated", + DeprecationWarning, + stacklevel=2) return self._loop @property diff --git a/aiohttp/web_app.py b/aiohttp/web_app.py index 6276b152c8a..82fefd3e4ae 100644 --- a/aiohttp/web_app.py +++ b/aiohttp/web_app.py @@ -159,6 +159,9 @@ def loop(self) -> asyncio.AbstractEventLoop: # Technically the loop can be None # but we mask it by explicit type cast # to provide more convinient type annotation + warnings.warn("loop property is deprecated", + DeprecationWarning, + stacklevel=2) return cast(asyncio.AbstractEventLoop, self._loop) def _set_loop(self, loop: Optional[asyncio.AbstractEventLoop]) -> None: @@ -331,7 +334,7 @@ def _make_handler(self, *, return Server(self._handle, # type: ignore request_factory=self._make_request, - loop=self.loop, **kwargs) + loop=self._loop, **kwargs) def make_handler(self, *, loop: Optional[asyncio.AbstractEventLoop]=None, diff --git a/aiohttp/web_fileresponse.py b/aiohttp/web_fileresponse.py index 2e75f0b7725..ccae5dac710 100644 --- a/aiohttp/web_fileresponse.py +++ b/aiohttp/web_fileresponse.py @@ -139,7 +139,7 @@ async def _sendfile_system(self, request: 'BaseRequest', else: writer = SendfileStreamWriter( request.protocol, - request.loop + request._loop ) request._payload_writer = writer diff --git a/aiohttp/web_request.py b/aiohttp/web_request.py index 39a1073a22b..f3631fbd2f3 100644 --- a/aiohttp/web_request.py +++ b/aiohttp/web_request.py @@ -212,6 +212,9 @@ def rel_url(self) -> URL: @reify def loop(self) -> asyncio.AbstractEventLoop: + warnings.warn("request.loop property is deprecated", + DeprecationWarning, + stacklevel=2) return self._loop # MutableMapping API diff --git a/aiohttp/web_ws.py b/aiohttp/web_ws.py index faf0cdae5b4..72dae0437eb 100644 --- a/aiohttp/web_ws.py +++ b/aiohttp/web_ws.py @@ -194,7 +194,7 @@ def _handshake(self, request: BaseRequest) -> Tuple['CIMultiDict[str]', notakeover) def _pre_start(self, request: BaseRequest) -> Tuple[str, WebSocketWriter]: - self._loop = request.loop + self._loop = request._loop headers, protocol, compress, notakeover = self._handshake( request) diff --git a/tests/test_client_connection.py b/tests/test_client_connection.py index e9de05a8be1..751044b6c8b 100644 --- a/tests/test_client_connection.py +++ b/tests/test_client_connection.py @@ -33,7 +33,8 @@ def protocol(): def test_ctor(connector, key, protocol, loop) -> None: conn = Connection(connector, key, protocol, loop) - assert conn.loop is loop + with pytest.warns(DeprecationWarning): + assert conn.loop is loop assert conn.protocol is protocol conn.close() diff --git a/tests/test_client_session.py b/tests/test_client_session.py index b1884ca346f..6c6b868e51c 100644 --- a/tests/test_client_session.py +++ b/tests/test_client_session.py @@ -475,7 +475,8 @@ async def test_session_default_version(loop) -> None: async def test_session_loop(loop) -> None: session = aiohttp.ClientSession(loop=loop) - assert session.loop is loop + with pytest.warns(DeprecationWarning): + assert session.loop is loop await session.close() diff --git a/tests/test_loop.py b/tests/test_loop.py index c0139aa8e79..25d36c706e1 100644 --- a/tests/test_loop.py +++ b/tests/test_loop.py @@ -25,11 +25,11 @@ async def get_application(self): return app async def on_startup_hook(self, app): - self.startup_loop = app.loop + self.on_startup_called = True @unittest_run_loop async def test_on_startup_hook(self) -> None: - self.assertIsNotNone(self.startup_loop) + self.assertTrue(self.on_startup_called) def test_default_loop(self) -> None: self.assertIs(self.loop, asyncio.get_event_loop()) diff --git a/tests/test_web_app.py b/tests/test_web_app.py index d335a914b26..6d54a305be1 100644 --- a/tests/test_web_app.py +++ b/tests/test_web_app.py @@ -14,7 +14,8 @@ async def test_app_ctor() -> None: loop = asyncio.get_event_loop() with pytest.warns(DeprecationWarning): app = web.Application(loop=loop) - assert loop is app.loop + with pytest.warns(DeprecationWarning): + assert loop is app.loop assert app.logger is log.web_logger @@ -25,14 +26,16 @@ def test_app_call() -> None: def test_app_default_loop() -> None: app = web.Application() - assert app.loop is None + with pytest.warns(DeprecationWarning): + assert app.loop is None async def test_set_loop() -> None: loop = asyncio.get_event_loop() app = web.Application() app._set_loop(loop) - assert app.loop is loop + with pytest.warns(DeprecationWarning): + assert app.loop is loop def test_set_loop_default_loop() -> None: @@ -40,7 +43,8 @@ def test_set_loop_default_loop() -> None: asyncio.set_event_loop(loop) app = web.Application() app._set_loop(None) - assert app.loop is loop + with pytest.warns(DeprecationWarning): + assert app.loop is loop asyncio.set_event_loop(None) @@ -48,7 +52,8 @@ def test_set_loop_with_different_loops() -> None: loop = asyncio.new_event_loop() app = web.Application() app._set_loop(loop) - assert app.loop is loop + with pytest.warns(DeprecationWarning): + assert app.loop is loop with pytest.raises(RuntimeError): app._set_loop(loop=object()) diff --git a/tests/test_web_request.py b/tests/test_web_request.py index cf4f6801d15..e4ac8ecdaac 100644 --- a/tests/test_web_request.py +++ b/tests/test_web_request.py @@ -1,3 +1,4 @@ +import asyncio import socket from collections.abc import MutableMapping from unittest import mock @@ -655,3 +656,10 @@ def test_eq() -> None: req2 = make_mocked_request('GET', '/path/to?a=1&b=2') assert req1 != req2 assert req1 == req1 + + +async def test_loop_prop() -> None: + loop = asyncio.get_event_loop() + req = make_mocked_request('GET', '/path', loop=loop) + with pytest.warns(DeprecationWarning): + assert req.loop is loop