FastAPI adapter¶
pip install pydepi-fastapi — import as depi_fastapi. Usage guide:
FastAPI integration.
autowire=True is rejected at construction and inject raises
NotImplementedError; resolve from the scope returned by get_scope instead.
FastAPIInjector
¶
FastAPIInjector(provider: ServiceProvider, param_name: str = 'provider', autowire: bool = False)
Bases: BaseInjector
Per-request dependency scopes for a FastAPI app.
Example
injector = FastAPIInjector(provider) injector.setup(app)
@app.get('/data/{key}') async def get_data(key: str, scope=Depends(injector.get_scope)): return scope.resolve(DataService).get(key)
Source code in packages/depi-fastapi/depi_fastapi/__init__.py
setup
¶
Install the ASGI scope middleware on app.
Source code in packages/depi-fastapi/depi_fastapi/__init__.py
get_scope
¶
get_scope() -> ServiceScope
FastAPI dependency returning the request scope.
Use as Depends(injector.get_scope).
inject
¶
Not supported. Use Depends(get_scope).
A wrapper built with functools.wraps sets __wrapped__, and
inspect.signature follows it by default -- so FastAPI would still
see the injected parameter and fail on it. There is no version of this
decorator that works without signature rewriting.
Source code in packages/depi-fastapi/depi_fastapi/__init__.py
create_scope
¶
create_scope() -> ServiceScope
current_scope
¶
current_scope() -> ServiceScope
Return the scope bound to the current request context.
Raises:
-
NoActiveScopeError–if setup(app) never ran, or this is called outside a request.