Skip to content

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

@Kludex

Description

@Kludex

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions