0

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
STY
  • 73
  • 1
  • 6
  • What is the actual line where the error is triggered? What is the actual definition of `get_db`? – MatsLindh Oct 04 '22 at 08:14
  • The line triggering the error is in the first code block, where the DB depends on get_db(). It is the call to get_db(), it doesn't reach inside the method. It's the call that is triggering this error. – STY Oct 04 '22 at 19:52
  • Yes, but _what_ is `get_db` returning? Does it return a function, or just a database? It seems like you're yielding the database connection, and not return a function that the dependency call can invoke. – MatsLindh Oct 04 '22 at 20:16
  • Just a database – STY Oct 04 '22 at 20:17
  • `Depends` needs a function to call (it expects a callable as you can see in the error). When you yield something else it doesn't know what to do with that - since you're now calling the function instead of giving it to `Depends`. So your `get_db` function needs to return a function that yields the database, where the string has now been bound to the inner function. – MatsLindh Oct 04 '22 at 20:26

1 Answers1

0

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.

STY
  • 73
  • 1
  • 6