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

functools.partial() does not work on an async dependable #10720

Open
9 tasks done
Kludex opened this issue Nov 26, 2023 Discussed in #9744 · 4 comments
Open
9 tasks done

functools.partial() does not work on an async dependable #10720

Kludex opened this issue Nov 26, 2023 Discussed in #9744 · 4 comments
Labels
investigate question Question or problem

Comments

@Kludex
Copy link
Sponsor Collaborator

Kludex commented Nov 26, 2023

Discussed in #9744

Originally posted by lieryan June 26, 2023

First Check

  • I added a very descriptive title here.
  • I used the GitHub search to find a similar question and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from functools import partial

from fastapi import FastAPI, Depends


app = FastAPI()

def the_answer():
    return 42


async def dep1(
    n: int = Depends(the_answer),
):
    return n + 1

@app.get("/")
def main(
    d1=Depends(partial(dep1, n=10)),
):
    print(d1)   # prints "<coroutine object dep1 at 0x7facc996fb50>"
    return {}


if __name__ == '__main__':
    import uvicorn
    uvicorn.run(app, port=1122)

Description

I would've expected d1 above to be the return value of the dependable, but it seems like fastapi didn't identify the partial as an coroutine function and instead passed the unawaited coroutine into the view function.

inspect.iscoroutinefunction() and inspect.signature() were able to deconstruct the partial object to correctly identify whether a partial contains a coroutine or a regular function, so it seems like it should've been possible for FastAPI to use handle partial correctly:

In [5]: def regular(a, b): pass

In [6]: async def coro(a, b): pass

In [7]: inspect.iscoroutinefunction(partial(regular, 1))
Out[7]: False

In [8]: inspect.iscoroutinefunction(partial(coro, 1))
Out[8]: True

In [20]: inspect.signature(partial(coro, 1))
Out[20]: <Signature (b)>

Operating System

Linux

Operating System Details

No response

FastAPI Version

0.98.0

Python Version

Python 3.10.6

Additional Context

No response

@Kludex Kludex added question Question or problem investigate labels Nov 26, 2023
@dayeondev
Copy link

I understand that using an asynchronous function without await in a synchronous function will naturally result in a coroutine object. Could you please clarify the intention behind your question? It seems evident that this behavior is expected in Python's asynchronous programming model.

@knight2995
Copy link

I understand that using an asynchronous function without await in a synchronous function will naturally result in a coroutine object. Could you please clarify the intention behind your question? It seems evident that this behavior is expected in Python's asynchronous programming model.

In my opinion,in FastAPI, dependency functions are automatically executed with await added.
This is based on the documentation

@atapin
Copy link

atapin commented Dec 29, 2023

@Kludex it works as expected with yield:

async def dep1(
    n: int = Depends(the_answer),
):
    yield n + 1

Output 1:

@app.get("/")
def main(
    d1=Depends(partial(dep1)),
):
    print(d1)  # prints 43
    return {}

Output 2:

@app.get("/")
def main(
    d1=Depends(partial(dep1, n=10)),
):
    print(d1)  # prints 11
    return {}

@lieryan
Copy link

lieryan commented Jan 22, 2024

There is a pull request that has the test cases and the fix for this issue: #9753.

@atapin yeah, async generator dependency works, but it doesn't work for async regular function dependency.

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

No branches or pull requests

5 participants