Skip to content

Conversation

@lbhsot
Copy link

@lbhsot lbhsot commented Jun 8, 2021

Hi, it seems wrong when using async partial function in Depends. Here's an example code:

def partial_func(q: str):
    async def inner(data):
        return data

    return partial(inner, q + " extras")

@app.get("/")
def test(data: str = Depends(partial_func("test"))):
    return {"data": data}

In test method, data here will be a coroutine object.

I just add asyncio.iscoroutinefunction to check if depends function is awaitable in this pr, I have no idea whether this is the right solution. Or maybe I need to change is_coroutine_callable method?

@lbhsot lbhsot marked this pull request as draft June 8, 2021 04:56
@lbhsot lbhsot marked this pull request as ready for review June 8, 2021 05:14
@lbhsot
Copy link
Author

lbhsot commented Jun 10, 2021

I found a workaround for this issue: Using A callable instance instead of partial function.

class PartialCls:
    def __init__(self, q: str):
        self.q = q

    async def __call__(self):
        return self.q + "extras"


@app.get("/")
def test(data: str = Depends(PartialCls("test"))):
    return {"data": data}

So I don't know whether we should use partial function or not, but since there is a solution here, I'll close this pr for now.
If we need use async partial function, maybe open another issue to track this bug(or feature?)

@lbhsot lbhsot closed this Jun 10, 2021
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

Successfully merging this pull request may close these issues.

2 participants