question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Override dependencies that dont return nothing

See original GitHub issue

Hi… 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:closed
  • Created 2 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Soufrazcommented, Jul 24, 2021

Found! In tests file I was importing …main and …api.dependencies Changed to app.main and app.api.dependencies. Thank you guys!

1reaction
Mausecommented, Jul 24, 2021

I can’t see any issues with this code - here’s a full example that works fine:

from fastapi import FastAPI, Depends
from fastapi.testclient import TestClient


app = FastAPI()


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)


async def database_check_pass():
    return "the value were expecting"


app.dependency_overrides[database_check] = database_check_pass


@app.get("/")
def route(database_check=Depends(database_check)):
    return database_check


tc = TestClient(app)
assert tc.get("/").json() == "the value were expecting"
Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing Dependencies with Overrides - FastAPI
To override a dependency for testing, you put as a key the original dependency (a function), and as the value, your dependency override...
Read more >
python - Overriding FastAPI dependencies that have parameters
I use next fixtures for main db overriding to db for testing: from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine from ...
Read more >
Dependency override not working · Issue #5090 - GitHub
Description. I'm trying to run some tests with pytest against the API but my dependency override is not working.
Read more >
Overriding dependencies in the Angular injector hierarchy
Overriding Dependencies Angular Injector Hierarchy ... It creates a model to validate data that will be returned from the product's service.
Read more >
Understanding Dependencies in useEffect | Bits and Pieces
A deep dive into the dependencies array in React.useEffect(). ... If you don't specify it, the effect runs after each render. If it's...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found