From e50da3525283753dbfc6c6e9c569c959f7084968 Mon Sep 17 00:00:00 2001 From: Igor Davydenko Date: Fri, 27 Nov 2015 17:19:49 +0200 Subject: [PATCH 1/2] Rename `loader` keyword argument in `web.Request.json` method. This adds consistency to the library as: * `loads` alredy used in `client.Response.json` method * `dumps` used in `web.json_response` function (introduced in 0.19.0) --- aiohttp/web_reqrep.py | 4 ++-- docs/web_reference.rst | 4 ++-- tests/test_web_functional.py | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/aiohttp/web_reqrep.py b/aiohttp/web_reqrep.py index 8133cd79b54..3eb3f8a8ffe 100644 --- a/aiohttp/web_reqrep.py +++ b/aiohttp/web_reqrep.py @@ -326,10 +326,10 @@ def text(self): return bytes_body.decode(encoding) @asyncio.coroutine - def json(self, *, loader=json.loads): + def json(self, *, loads=json.loads): """Return BODY as JSON.""" body = yield from self.text() - return loader(body) + return loads(body) @asyncio.coroutine def post(self): diff --git a/docs/web_reference.rst b/docs/web_reference.rst index dd059306d78..bd535053936 100644 --- a/docs/web_reference.rst +++ b/docs/web_reference.rst @@ -250,14 +250,14 @@ like one using :meth:`Request.copy`. The method **does** store read data internally, subsequent :meth:`~Request.text` call will return the same value. - .. coroutinemethod:: json(*, loader=json.loads) + .. coroutinemethod:: json(*, loads=json.loads) Read request body decoded as *json*. The method is just a boilerplate :ref:`coroutine ` implemented as:: - async def json(self, *, loader=json.loads): + async def json(self, *, loads=json.loads): body = await self.text() return loader(body) diff --git a/tests/test_web_functional.py b/tests/test_web_functional.py index 8fab9d9b4a0..c7d2c231d90 100644 --- a/tests/test_web_functional.py +++ b/tests/test_web_functional.py @@ -142,7 +142,7 @@ def test_post_json(self): def handler(request): data = yield from request.json() self.assertEqual(dct, data) - data2 = yield from request.json() + data2 = yield from request.json(loads=json.loads) self.assertEqual(data, data2) resp = web.Response() resp.content_type = 'application/json' From 78fa7c322d7d62fb79ee679e6cc0e35dc5e163e9 Mon Sep 17 00:00:00 2001 From: Igor Davydenko Date: Fri, 27 Nov 2015 19:25:55 +0200 Subject: [PATCH 2/2] Add a DeprecationWarning, while using old `loader` arg. --- aiohttp/web_reqrep.py | 7 ++++++- tests/test_web_functional.py | 2 ++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/aiohttp/web_reqrep.py b/aiohttp/web_reqrep.py index 3eb3f8a8ffe..6995df15eb8 100644 --- a/aiohttp/web_reqrep.py +++ b/aiohttp/web_reqrep.py @@ -326,8 +326,13 @@ def text(self): return bytes_body.decode(encoding) @asyncio.coroutine - def json(self, *, loads=json.loads): + def json(self, *, loads=json.loads, loader=None): """Return BODY as JSON.""" + if loader: + warnings.warn( + 'Using `loader` is deprecated, use `loads` instead', + DeprecationWarning) + loads = loader body = yield from self.text() return loads(body) diff --git a/tests/test_web_functional.py b/tests/test_web_functional.py index c7d2c231d90..cd5b03077f9 100644 --- a/tests/test_web_functional.py +++ b/tests/test_web_functional.py @@ -144,6 +144,8 @@ def handler(request): self.assertEqual(dct, data) data2 = yield from request.json(loads=json.loads) self.assertEqual(data, data2) + data3 = yield from request.json(loader=json.loads) + self.assertEqual(data, data3) resp = web.Response() resp.content_type = 'application/json' resp.body = json.dumps(data).encode('utf8')