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

Detect where __call__ is async def in context of iscoroutinefunction() #123

Closed
peterschutt opened this issue May 31, 2022 · 2 comments
Closed
Assignees
Labels
Bug 🐛 This is something that is not working as expected

Comments

@peterschutt
Copy link
Contributor

My use-case is basically a guard that I create with some state:

@put(guards=[CheckPayloadMismatch("id", "user_id").__call__])

Without passing through the __call__() method, starlite tries to call the instance sync.

As of 0.20.1, starlette themselves include this utility:

import asyncio
import functools
import typing


def is_async_callable(obj: typing.Any) -> bool:
    while isinstance(obj, functools.partial):
        obj = obj.func

    return asyncio.iscoroutinefunction(obj) or (
        callable(obj) and asyncio.iscoroutinefunction(obj.__call__)
    )

And a handful of test cases: https://github.com/encode/starlette/blob/master/tests/test__utils.py

Based on a quick search, there are 6 separate usages of iscoroutinefunction(): https://github.com/starlite-api/starlite/search?q=iscoroutinefunction. The most complex pattern of use is:

https://github.com/starlite-api/starlite/blob/f9893f28bc210e032e0527a61a6b695fc9e5c801/starlite/handlers/asgi.py#L43

Which would collapse down to a single call to is_async_callable() if we adopt such a function.

I'm happy to do the work if you give it the green light.

@Goldziher
Copy link
Contributor

Go ahead, also - take my permission as granted

@peterschutt peterschutt self-assigned this Jun 17, 2022
@peterschutt peterschutt added the Bug 🐛 This is something that is not working as expected label Jun 17, 2022
@peterschutt
Copy link
Contributor Author

Closed by #162

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🐛 This is something that is not working as expected
Projects
None yet
Development

No branches or pull requests

2 participants