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

View and MethodView classes do not support async handlers #4112

miguelgrinberg opened this issue May 27, 2021 · 0 comments

View and MethodView classes do not support async handlers #4112

miguelgrinberg opened this issue May 27, 2021 · 0 comments


Copy link

miguelgrinberg commented May 27, 2021

Regular views can now be written as async functions, but instances of the View and MethodView classes did not get updated to support this functionality. Consider the following example:

import asyncio
from flask import Flask
from flask.views import View, MethodView

app = Flask(__name__)

class Test(View):
    async def dispatch_request(self):
        await asyncio.sleep(1)
        return 'test'

class Test2(MethodView):
    async def get(self):
        await asyncio.sleep(1)
        return 'test2'

app.add_url_rule('/test', view_func=Test.as_view('test'))
app.add_url_rule('/test2', view_func=Test2.as_view('test2'))

The expectation is that you should be able to send requests to /test and /test2 and that their respective handlers should be wrapped with the app.ensure_sync() method, as function-based handlers are.

Instead sending requests to these URLs cause the following error:

TypeError: The view function did not return a valid response. The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a coroutine.


  • Python version: >= 3.7
  • Flask version: >= 2
@greyli greyli closed this as completed Jun 1, 2021
@greyli greyli added this to the 2.0.2 milestone Jun 4, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 19, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet
None yet

No branches or pull requests

2 participants