FastAPI Post request with database depends, object is not callable

2.2k Views Asked by At

For some reason, I am unable to pass a parameter and I get the following error, however, if I were to remove the string, everything behaves properly. If anyone else has come across a such an error please let me know how to resolve it!

This is what my API looks like:

@router.post("/post")
async def post_request(
     db: Session = Depends(database.get_db(string_here="string")
):
    return ...

and a database file with

def get_db(string_here: str):
    .... returns a db

ERROR:

raise TypeError('{!r} is not a callable object'.format(obj))
TypeError: <generator object get_db at 0x000001FCFD7A8900> is not a callable object
1

There are 1 best solutions below

0
STY On

This seems to have resolved my problem. I don't fully understand why it works, but it does. If someone wants to clarify the logic behind it, I would appreciate it.

https://stackoverflow.com/a/12074749/18347899

You don't need to call your generator, remove the () brackets.

You are probably confused by the fact that you use the same name for the variable inside the function as the name of the generator; the following will work too:

def somefun(lengen):
    for length in lengen:
        if not is_blahblah(length): return False

A parameter passed to the somefun function is then bound to the local lengen variable instead of lengths, to make it clear that that local variable is not the same thing as the lengths() function you defined elsewhere.