Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable use of await for a class based view #714

Closed
wants to merge 1 commit into from

Conversation

mattrasband
Copy link

Currently if you implement a middleware in the 3.5 syntax (native coroutines, e.g. async def and await) for class based views, they will yield a TypeError saying that that web.View is not awaitable. This is due to a new magic method __await__ for classes that needs to be implemented. This change simply uses the current __iter__ behavior (supporting 3.4 @asyncio.coroutine and yield from) and enables the 3.5 await keyword (required for native coroutines, e.g. async def)

A quick example of an error this can cause is in middleware written in the 3.5 syntax:

def db_middleware(dsn):
    async def factory(app, handler):
        async def middleware(request):
            db = app.get('db')
            if not db:
                app['db'] = db = await create_engine(dsn)
            request.app['db'] = db
            response = await handler(request)
        return middleware
    return factory

Without the __await__ method implemented a TypeError is raised.

Validated to work with 3.4+

@asvetlov
Copy link
Member

Please add tests

@mattrasband
Copy link
Author

@asvetlov Roger, I meant to ask - is there a good way to test python 3.4 vs 3.5 in the current setup? I saw that appveyor does it but wasn't sure the right way to try it locally?

asvetlov added a commit that referenced this pull request Jan 3, 2016
@asvetlov
Copy link
Member

asvetlov commented Jan 3, 2016

Fixed by #717
@mrasband see the PR for test example.
Thanks for raising the issue

@asvetlov asvetlov closed this Jan 3, 2016
@mattrasband
Copy link
Author

Awesome, you're the man. Sorry, I've been traveling a bit recently and haven't gotten back to doing it.

asvetlov added a commit that referenced this pull request Jan 3, 2016
@lock
Copy link

lock bot commented Oct 29, 2019

This thread has been automatically locked since there has not been
any recent activity after it was closed. Please open a new issue for
related bugs.

If you feel like there's important points made in this discussion,
please include those exceprts into that new issue.

@lock lock bot added the outdated label Oct 29, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 29, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants