skip to content

A FastAPI test overrides the lru_cache-wrapped get_settings, yet part of the app still uses production values; what does the override not reach?

level: seniorimportance: should knowfreq 33%

answer

  1. only what Depends resolves
  2. import-time and direct calls
  3. the cache already holds the first value
  4. env vars plus cache_clear()

basics

~20 s

An override of get_settings only reaches values FastAPI resolves through Depends during a request. Direct get_settings() calls at import, in the lifespan or in helpers still run the real lru_cache function and keep the production Settings.

solid answer

~40 s

The override is keyed by the decorated `get_settings`, the `@lru_cache` wrapper that `Depends` received, and FastAPI applies it while resolving a request's dependencies: endpoint parameters and sub-dependencies such as a payment client built from settings. It cannot reach code that calls `get_settings()` directly. A module-level `create_engine(get_settings().database_url)`, the lifespan, middleware configuration or a helper function all run the real function, and `lru_cache` keeps the first `Settings` object for the rest of the process. For those paths a test sets environment variables and calls `get_settings.cache_clear()` before the code runs, or the app is restructured so the value arrives through `Depends` or an app factory that takes settings.

code

python · 33 lines
python
from functools import lru_cache
from typing import Annotated

from fastapi import Depends, FastAPI
from fastapi.testclient import TestClient
from pydantic_settings import BaseSettings


class Settings(BaseSettings):
    payments_url: str = "https://payments.example.com"
    database_url: str = "sqlite:///./app.db"


@lru_cache
def get_settings() -> Settings:
    return Settings()


DATABASE_URL = get_settings().database_url  # read once at import: no override reaches it

app = FastAPI()


@app.get("/config")
def config(settings: Annotated[Settings, Depends(get_settings)]):
    return {"payments_url": settings.payments_url, "database_url": DATABASE_URL}


app.dependency_overrides[get_settings] = lambda: Settings(
    payments_url="http://payments.test", database_url="sqlite://"
)
print(TestClient(app).get("/config").json())
app.dependency_overrides.clear()

go deeper

for a junior

Recall that settings are overridden like any dependency, keyed by the decorated get_settings, and that the override returns a new Settings object.

for a middle

Explain that the override acts only during dependency resolution, so direct calls and import-time values keep the cached production Settings.

for a senior

Diagnose a split between overridden and real settings by finding direct calls and import-time reads, and fix them with dependencies, a factory or cache_clear().

for a principal

Set the convention that request-time code receives settings only through dependencies and construction-time code only through an app factory, so tests have one override point.

## The usual settings pattern FastAPI's documentation recommends reading configuration through a dependency: ```python @lru_cache def get_settings(): return Settings() @app.get("/info") async def info(settings: Annotated[Settings, Depends(get_settings)]): ... ``` `Settings` is a pydantic-settings `BaseSettings` class that reads environment variables. `@lru_cache` makes `get_settings()` build it once and return the same object afterwards. Because it is a dependency, a test can replace it: ```python app.dependency_overrides[get_settings] = lambda: Settings(payments_url="http://payments.test") ``` The key is the decorated `get_settings`, the `lru_cache` wrapper, because that is the object passed to `Depends`. The override returns a new `Settings`; keyword arguments passed to `Settings(...)` take priority over environment variables, so the test controls those fields regardless of the machine's environment. ## What the override does reach FastAPI consults `app.dependency_overrides` while it resolves a request's dependency graph. So the override changes: - every endpoint parameter declared with `Depends(get_settings)`; - every dependency that itself depends on `get_settings`, for example `get_payment_client(settings: Annotated[Settings, Depends(get_settings)])`, so the payment client is built against the test URL; - dependencies declared on routers or decorators that use settings. In all of these FastAPI calls the override instead of the cached function; the `lru_cache` never answers. ## What it does not reach Anything that calls `get_settings()` as a plain function never passes through dependency resolution: 1. **Import-time code.** `engine = create_engine(get_settings().database_url)` in `db.py` runs when the module is imported, before any test sets an override, and the cache keeps that first `Settings`. 2. **The lifespan.** Startup code that reads `get_settings()` to load a model or open a pool runs the real function. 3. **Middleware and app construction.** `CORSMiddleware` origins or `FastAPI(docs_url=...)` computed from settings are fixed when the app object is built. 4. **Helpers called from endpoints.** A service function that calls `get_settings()` itself, instead of receiving settings as an argument, bypasses the override even though the endpoint that calls it was overridden. | Where settings are read | Reached by the override? | What a test can do | |---|---|---| | `Depends(get_settings)` in an endpoint or dependency | yes | override | | direct call inside a request helper | no | pass settings in, or override plus refactor | | lifespan startup | no | env vars and `get_settings.cache_clear()` before the client starts | | module import | no | env vars before import, or an app factory | ## Returning a fixed object instead of a new one The override above builds a new `Settings` on every request, because FastAPI caches a dependency's result for one request only. That is harmless in tests, but when a test wants to inspect the exact object the app used, build it once in a fixture and register `lambda: test_settings`. The fixture then owns the object, and the same fixture removes the override in teardown, like any other entry in `app.dependency_overrides`. ## Diagnosing the mismatch The symptom is a split brain: the endpoint returns the test value, while the database file, the outbound URL or the CORS policy is the production one. The example below shows it in a few lines: the overridden `payments_url` comes back from the endpoint, while `DATABASE_URL`, captured at import, still shows the file-based URL. When that happens, search the code for direct calls to `get_settings()` outside `Depends`, and check what ran at import time. ## Fixing it properly - **Make settings flow through dependencies.** Values used per request should arrive via `Depends(get_settings)` or via a dependency built on it, so one override changes all of them. - **Use an app factory for construction-time values.** `create_app(settings)` lets a test build an app with test settings, so middleware, docs URLs and the engine are created from them. - **Reset the cache when a direct call is unavoidable.** Set environment variables with pytest's `monkeypatch.setenv`, call `get_settings.cache_clear()` so the next direct call rebuilds `Settings`, and clear it again in teardown so later tests do not inherit the test values. ## Summary `app.dependency_overrides[get_settings]` swaps settings for everything FastAPI resolves during a request and nothing else. Code that reads settings at import, at startup or through direct calls needs environment variables and `cache_clear()`, or better, a structure in which settings arrive through dependencies or a factory.

  • Does the lru_cache on get_settings stop the override from working?
    No. The cache only answers when `get_settings()` itself is called. With an override registered, FastAPI calls the replacement instead of the wrapper, so the cached object is never consulted for that dependency. The key must be the decorated wrapper, which is what `Depends` received.
  • Why call get_settings.cache_clear() in teardown as well as setup?
    After the test, the cache holds a `Settings` built from the test's environment variables. Without clearing it again, any later direct call in the same process returns those test values, a leak just like a forgotten entry in `app.dependency_overrides`.

  • Overriding get_settings changes every place that ever calls get_settings().
  • The lru_cache answers before FastAPI checks dependency_overrides, so the override is ignored.
  • The override key must be the undecorated function inside the lru_cache wrapper.
  • Values captured at import time pick up the override on the next request.
  • Setting environment variables in a test is enough even after get_settings() has run once.