Skip to content

Enhancement: Dependency Override #1787

@kayprogrammer

Description

@kayprogrammer

Summary

Can you please add a dependency override to the application. Something similar to fastapi's app.dependency_overrides.
This is important when trying to override a dependency while writing tests.

Basic Example

@pytest.fixture
def client(app):
    database = TestAsyncSessionLocal()
    def test_get_db(app):
        try:
            yield database
        finally:
            database.close()
    app.dependency_overrides['db'] = Provide(test_get_db) # nothing like dependency_overrides in starlite
    ...

Drawbacks and Impact

Here's what I tried:

def client(app):
    database = TestAsyncSessionLocal()
    def test_get_db(app):
        try:
            yield database
        finally:
            database.close()
    app.dependencies['db'] = Provide(test_get_db)
    ...

This doesn't work because handlers are already registered in main:app. So overriding dependencies like this after handlers have been registered changes nothing.

Unresolved questions

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionThis is a question and further information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions