Override dependencies that dont return nothing
See original GitHub issueHi… I started this discussion on discord but seems like a more tricky thing.
I’d like to know if dependency_overrides works when the dependency function dont return anything… in my case my dependency function only pass if not wrong happens or throw an httpexception if something wrong happens… The dependency override is not working because in fastapi.dependencies.utils, when the code will try override the dependencies, the reference of functions are different and then the override dont occurs…
My dependency:
async def database_check():
try:
_database_is_alive()
if not database.is_connected:
await database.connect()
except ConnectionDatabaseError:
if database.is_connected:
await database.disconnect()
raise HTTPException(status_code=503)
My 3 attempts functions to override dependency:
from ..api.dependencies import database_check
async def database_check_pass():
return True
async def database_check_pass():
pass
async def database_check_pass():
def handle():
return True
return handle
My override dependency code:
app.dependency_overrides[database_check] = database_check_pass
Something I notice, inside fastapi code, the function reference inside dependency_overrides_provider is different than the original function in app/api/dependencies.py
fastapi.dependencies.utils
call = getattr(
dependency_overrides_provider, "dependency_overrides", {}
).get(original_call, original_call)
My app structure:
app/
api/
dependencies.py
views.py
tests/
my_test.py
main.py
What am I missing here?
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (2 by maintainers)
Top GitHub Comments
Found! In tests file I was importing …main and …api.dependencies Changed to app.main and app.api.dependencies. Thank you guys!
I can’t see any issues with this code - here’s a full example that works fine: