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.

Accessing the `app` in `Depends`

See original GitHub issue

I would like to access the FastAPI app in a Depends function, is there a way to do that? The rationale here is that I would like to avoid global variables and use the state in app.

Example usecase:

def get_session(app: fastapi.FastAPI) -> Session:
    [...]
    access the app.state here
    [...]

@router.post("/init")
def create(session: Session = Depends(get_session)):
[...]
use the SQLAlchemy session
[...]

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
chbndrhnnscommented, May 10, 2021

This works for me:

from fastapi import FastAPI, Depends
from starlette.requests import Request
from starlette.testclient import TestClient

app = FastAPI()


async def dep(request: Request):
    request.app.state.called = True


@app.get("/me", dependencies=[Depends(dep)])
async def me(request: Request):
    return {"called": request.app.state.called}


def test_dep():
    client = TestClient(app)
    res = client.get("/me")
    assert res.json()["called"]

0reactions
tiangolocommented, Dec 19, 2022

Thanks for the help here @chbndrhnns ! 👏 🙇

Thanks for reporting back and closing the issue @nicolaerosia 👍

Sorry for the long delay! 🙈 I wanted to personally address each issue/PR and they piled up through time, but now I’m checking each one in order.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is Application Dependency? | Palo Alto Networks
The "Implicitly Use Applications" is a subset of "Depends on," and the dependent applications are implicitly allowed. The application still does ...
Read more >
Manual dependency injection - Android Developers
Basics of manual dependency injection​​ It walks through an iterated approach of how you might start using dependency injection in your app. The ......
Read more >
Dependencies - First Steps - FastAPI - tiangolo
Declare the dependency, in the "dependant"¶. The same way you use Body , Query , etc. with your path operation function parameters, use...
Read more >
Dependency Walker (depends.exe) Home Page
Dependency Walker is a free utility that scans any 32-bit or 64-bit Windows module (exe, dll, ocx, sys, etc.) and builds a hierarchical...
Read more >
FastAPI Depends use-case application - python - Stack Overflow
@app.on_event("startup") async def start(): parser = ArgumentParser(. ... and overwrite a property declared inside the class to be accessed ...
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