Aiohttp: Uses class methods with aiohttp.web.RouteTableDef

Created on 14 May 2018  路  8Comments  路  Source: aio-libs/aiohttp

Long story short

Whenever you place a route as part of a class, it does not allow this to be possible, unless you remove te response argument, leaving just self (which creates problems as self is actually response).

Expected behaviour

Provide the webpage as it would like this:

from aiohttp import web
import asyncio

routes = web.RouteTableDef()
app = web.Application()

@routes.get("/")
async def root(response):
    return web.Response(text="5")

app.add_routes(routes)
web.run_app(app)

Actual behaviour

Errors with

Error handling request
Traceback (most recent call last):
  File "D:\Program Files\Python3\lib\site-packages\aiohttp\web_protocol.py", line 385, in start
    resp = await self._request_handler(request)
  File "D:\Program Files\Python3\lib\site-packages\aiohttp\web_app.py", line 338, in _handle
    resp = await handler(request)
TypeError: root() missing 1 required positional argument: 'response'

Steps to reproduce

Create and run this script

from aiohttp import web
import asyncio

routes = web.RouteTableDef()
app = web.Application()

class Example:
    @routes.get("/")
    async def root(self, response):
        return web.Response(text="5")

app.add_routes(routes)
web.run_app(app)

Your environment

Latest version of aiohttp (3.2.1)
Windows 10

enhancement outdated server

All 8 comments

In your example, web handler is not instantiated on decorator appliance time.
The decorated function is just a function, not bound method.

Maybe we can master something but I don't see a clean implementation yet.
Any idea is welcome.

Closing the issue.
If somebody wants to propose a robust implementation -- please open a new one.

I have been thinking of an implementation, I just haven't got round to implementing it yet. Here's the basic outline:

  • Create an alternative ClassRouteTableDef which returns a RouteTableDef class object
  • You can add this by doing something along the lines of add_routes(ClassWhichContainsTheRouteObjects())
  • Which can be run through and instantiated normally (as they now have a class)

Sorry for the late reply.

Feel free to make a PR with an implementation of your idea.

I will when done 馃槣!

Did anyone ever end up working on this? Would be a nice feature for sure.

I've got some in-progress code sitting on my desktop, I've mostly forgotten about it.

EDIT: This seems similar to https://github.com/dask/dask/issues/3317.

I've got a working implementation, will attempt some documentation (and possibly tests) and then do a PR.
The dask/dask#3317 issue only appears to be an issue for decorators that are classes and when actually replacing the decorated function.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JulienPalard picture JulienPalard  路  3Comments

amsb picture amsb  路  3Comments

rubenvdham picture rubenvdham  路  5Comments

deckar01 picture deckar01  路  4Comments

Smosker picture Smosker  路  3Comments