diff --git a/CHANGES/4175.bugfix b/CHANGES/4175.bugfix new file mode 100644 index 00000000000..c8a20753352 --- /dev/null +++ b/CHANGES/4175.bugfix @@ -0,0 +1 @@ +Fix the string representation of `ServerDisconnectedError`. diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 9a8979f2c0f..b7e05b07a13 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -129,6 +129,7 @@ Jake Davis Jakob Ackermann Jakub Wilk Jashandeep Sohi +Jens Steinhauser Jeongkyu Shin Jeroen van der Heijden Jesus Cea diff --git a/aiohttp/client_exceptions.py b/aiohttp/client_exceptions.py index 2738611b205..5257b0be1b8 100644 --- a/aiohttp/client_exceptions.py +++ b/aiohttp/client_exceptions.py @@ -160,11 +160,11 @@ class ServerDisconnectedError(ServerConnectionError): """Server disconnected.""" def __init__(self, message: Optional[str]=None) -> None: - self.message = message if message is None: - self.args = () - else: - self.args = (message,) + message = 'Server disconnected' + + self.args = (message,) + self.message = message class ServerTimeoutError(ServerConnectionError, asyncio.TimeoutError): diff --git a/tests/test_client_exceptions.py b/tests/test_client_exceptions.py index e85ce672e83..16d8fface5a 100644 --- a/tests/test_client_exceptions.py +++ b/tests/test_client_exceptions.py @@ -178,7 +178,7 @@ def test_str(self) -> None: class TestServerDisconnectedError: def test_ctor(self) -> None: err = client.ServerDisconnectedError() - assert err.message is None + assert err.message == 'Server disconnected' err = client.ServerDisconnectedError(message='No connection') assert err.message == 'No connection' @@ -194,7 +194,12 @@ def test_pickle(self) -> None: def test_repr(self) -> None: err = client.ServerDisconnectedError() - assert repr(err) == "ServerDisconnectedError()" + if sys.version_info < (3, 7): + assert repr(err) == ("ServerDisconnectedError" + "('Server disconnected',)") + else: + assert repr(err) == ("ServerDisconnectedError" + "('Server disconnected')") err = client.ServerDisconnectedError(message='No connection') if sys.version_info < (3, 7): @@ -204,7 +209,7 @@ def test_repr(self) -> None: def test_str(self) -> None: err = client.ServerDisconnectedError() - assert str(err) == '' + assert str(err) == 'Server disconnected' err = client.ServerDisconnectedError(message='No connection') assert str(err) == 'No connection' diff --git a/tests/test_client_functional.py b/tests/test_client_functional.py index a8372754364..656a14e2b9a 100644 --- a/tests/test_client_functional.py +++ b/tests/test_client_functional.py @@ -2680,10 +2680,12 @@ async def handler(request): app.router.add_get('/', handler) client = await aiohttp_client(app) - with pytest.raises(aiohttp.ServerDisconnectedError): + with pytest.raises(aiohttp.ServerDisconnectedError) as excinfo: resp = await client.get('/') await resp.read() + assert str(excinfo.value) != '' + async def test_dont_close_explicit_connector(aiohttp_client) -> None: async def handler(request):