Skip to content

Commit

Permalink
Deprecate .loop property (#3374)
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov committed Nov 2, 2018
1 parent 583cf94 commit 8743741
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGES/3374.removal
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Deprecate ``app.loop``, ``request.loop``, ``client.loop`` and ``connector.loop`` properties.
3 changes: 3 additions & 0 deletions aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions aiohttp/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion aiohttp/web_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion aiohttp/web_fileresponse.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ async def _sendfile_system(self, request: 'BaseRequest',
else:
writer = SendfileStreamWriter(
request.protocol,
request.loop
request._loop
)
request._payload_writer = writer

Expand Down
3 changes: 3 additions & 0 deletions aiohttp/web_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion aiohttp/web_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion tests/test_client_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
3 changes: 2 additions & 1 deletion tests/test_client_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down
4 changes: 2 additions & 2 deletions tests/test_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
15 changes: 10 additions & 5 deletions tests/test_web_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -25,30 +26,34 @@ 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:
loop = asyncio.new_event_loop()
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)


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())
Expand Down
8 changes: 8 additions & 0 deletions tests/test_web_request.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import socket
from collections.abc import MutableMapping
from unittest import mock
Expand Down Expand Up @@ -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

0 comments on commit 8743741

Please sign in to comment.