Skip to content

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
def __init__(
    self,
    provider: 'ServiceProvider',
    param_name: str = 'provider',
    autowire: bool = False
):
    if autowire:
        raise ValueError(
            "FastAPIInjector does not support autowire=True. FastAPI builds "
            "request parsing and the OpenAPI schema from the endpoint "
            "signature, and raises FastAPIError at decoration time for any "
            "annotation it cannot treat as a Pydantic field. Hiding service "
            "parameters would mean rewriting __signature__ before FastAPI "
            "sees the endpoint, which is fragile across FastAPI releases. "
            "Use Depends(injector.get_scope) instead."
        )
    super().__init__(provider, param_name=param_name, autowire=autowire)

provider property

provider: ServiceProvider

The provider this injector resolves from.

setup

setup(app) -> None

Install the ASGI scope middleware on app.

Source code in packages/depi-fastapi/depi_fastapi/__init__.py
def setup(self, app) -> None:
    """Install the ASGI scope middleware on ``app``."""
    provider = self._provider

    class _DepiScopeMiddleware:
        # The parameter must be named `app`: Starlette <= 0.27 instantiates
        # middleware as cls(app=..., **options) with app as a KEYWORD
        # argument, while newer Starlette passes it positionally. Naming it
        # anything else breaks every FastAPI at or below the declared floor.
        def __init__(self, app):
            self.app = app

        async def __call__(self, scope, receive, send):
            if scope['type'] not in ('http', 'websocket'):
                await self.app(scope, receive, send)
                return

            di_scope = provider.create_scope()
            token = set_current_scope(di_scope)
            try:
                await self.app(scope, receive, send)
            finally:
                reset_current_scope(token)
                await di_scope.__aexit__(None, None, None)

    app.add_middleware(_DepiScopeMiddleware)

get_scope

get_scope() -> ServiceScope

FastAPI dependency returning the request scope.

Use as Depends(injector.get_scope).

Source code in packages/depi-fastapi/depi_fastapi/__init__.py
def get_scope(self) -> 'ServiceScope':
    """
    FastAPI dependency returning the request scope.

    Use as ``Depends(injector.get_scope)``.
    """
    return self.current_scope()

inject

inject(fn)

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
def inject(self, fn):
    """
    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.
    """
    raise NotImplementedError(
        "FastAPIInjector.inject is not supported; FastAPI reads the endpoint "
        "signature to build request parsing and the OpenAPI schema. "
        "Use Depends(injector.get_scope) and resolve from the scope."
    )

create_scope

create_scope() -> ServiceScope

Create a new, unbound dependency scope.

Source code in packages/depi-core/depi/integration.py
def create_scope(self) -> 'ServiceScope':
    """Create a new, unbound dependency scope."""
    return self._provider.create_scope()

current_scope

current_scope() -> ServiceScope

Return the scope bound to the current request context.

Raises:

Source code in packages/depi-core/depi/integration.py
def current_scope(self) -> 'ServiceScope':
    """
    Return the scope bound to the current request context.

    Raises:
        NoActiveScopeError: if setup(app) never ran, or this is called
            outside a request.
    """
    return current_scope()