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

Allow callback= to be an awaitable function #4

Closed
simonw opened this issue May 7, 2019 · 1 comment
Closed

Allow callback= to be an awaitable function #4

simonw opened this issue May 7, 2019 · 1 comment
Labels
good first issue Good for newcomers

Comments

@simonw
Copy link
Owner

simonw commented May 7, 2019

If callback= could be provided an awaitable, users could construct functions that e.g. make an async call to a database as part of deciding if a host should be allowed.

I tried implementing this with a asyncio.isawaitable() test against the callable but for some reason it didn't detect my await function as being awaitable. Here's the unit test I added which I failed to get passing:

@pytest.mark.asyncio
async def test_callback_async():
    was_called = False

    async def callback_true(origin):
        return True

    async def callback_false(origin):
        return False

    assert EXAMPLE_HOST == await get_cors_header(asgi_cors(hello_world_app, callback=callback_true), EXAMPLE_HOST)
    assert None == await get_cors_header(asgi_cors(hello_world_app, callback=callback_false), EXAMPLE_HOST)
@dmgolembiowski
Copy link

@simonw Admittedly, this might not be much help to you (or relevant anymore) but would changing asyncio.iscoroutine(...) to asyncio.iscoroutinefunction(...) fix the issue?

@simonw simonw closed this as completed Apr 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants