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

async function support in url_defaults and url_value_preprocessor #315

Open
mozcelebi opened this issue Jan 7, 2024 · 1 comment
Open

Comments

@mozcelebi
Copy link

currently if you try to use an async function in url_defaults or url_value_preprocessor it raises

.venv/lib/python3.10/site-packages/quart/app.py:1450: RuntimeWarning: coroutine 'pull_lang_code' was never awaited

error.

The following code uses get_preferred_language from Quart-Babel extension but it could also be an async db call in an url_value_preprocessor function.

@app.url_defaults
async def add_language_code(endpoint, values):
    if app.url_map.is_endpoint_expecting(endpoint, "lang_code"):
        prefered_lang = await get_preferred_language()
        values.setdefault("lang_code", prefered_lang)

do you have any workaround/suggestions to this situation?

@mozcelebi
Copy link
Author

I think by using the workaround mentioned in https://quart.palletsprojects.com/en/latest/discussion/async_compatibility.html this can be solved, but is looks dirty.

After adding quart flask patch

import quart_flask_patch

the following code works.

@app.url_defaults
def add_language_code(endpoint, values):
    if app.url_map.is_endpoint_expecting(endpoint, "lang_code"):
        prefered_lang = asyncio.get_event_loop().sync_wait(get_preferred_language())
        values.setdefault("lang_code", prefered_lang)

any possible roadmap to make these annotations accept not just callable but also awaitable functions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant