API Router Depends Not Compatible with Depend()

Viewed 196

Description

I am writing a FastAPI app for making multiplayer chess games. But unfortunately I am stuck on a circular import problem (or so I am guessing) and I'm not able to move forward. Basically I have a socket manager variable in the api/main.py which needs to be imported in api/endpoints/game.py, but in api/main.py I also include the API routers. So whenever I include the API routers it would call that module, which would then call api/main.py which would again call game.py to include the router, therefore creating a circular imports problem. This is the complete python traceback: here.

Code

The code can be seen on GitHub, here it the repository link https://github.com/p0lygun/astounding-arapaimas/tree/feature/api/games/api.

My Tries

I tried all the "hacks" of injecting the module using sys and os but nope they too caused the same problem. I tried moving the socket_manager into a completely different file too but that would too cause the same problem.

If any other information is required let me know, posting a question for the first time so not so sure what all is needed. Thanks!

Edits

Edit 1

codemation#0324 on discord suggested a better way to access the socket manager like this:

enter image description here

Interestingly I get the same error afterwards too.

Edit 2

The problem is happening along the lines where I add JWT Auth depend into the API Router and then use Depend(get_db) in the endpoint definition function. An alternative would be not Depend the get_db() and just call it normally and pass it on to the CRUD functions for the timing.

To be clear, when I remove , dependencies=[Depends(auth.JWTBearer())] it works perfectly.

0 Answers
Related