from typing import List from fastapi import Depends, FastAPI from typing_extensions import Self class MyDatabaseConnection: """ This is a mock just for example purposes. """ async def __aenter__(self) -> Self: return self async def __aexit__(self, exc_type, exc_val, exc_tb): pass async def get_records(self, table_name: str) -> List[dict]: pass app = FastAPI() async def get_database_connection(): async with MyDatabaseConnection() as connection: yield connection GlobalDatabaseConnection = Depends(get_database_connection, dependency_scope="lifespan") @app.get("/users/") async def read_users( database_connection: MyDatabaseConnection = GlobalDatabaseConnection, ): return await database_connection.get_records("users") @app.get("/items/") async def read_items( database_connection: MyDatabaseConnection = GlobalDatabaseConnection, ): return await database_connection.get_records("items")