Home / File/ test_dependency_contextvars.py — fastapi Source File

test_dependency_contextvars.py — fastapi Source File

Architecture documentation for test_dependency_contextvars.py, a python file in the fastapi codebase. 5 imports, 0 dependents.

File python FastAPI Responses 5 imports 4 functions

Entity Profile

Dependency Diagram

graph LR
  e41999b9_ee75_d78c_cb43_68601b39ae25["test_dependency_contextvars.py"]
  07d79a2e_d4e9_0bbb_be90_936274444c8c["collections.abc"]
  e41999b9_ee75_d78c_cb43_68601b39ae25 --> 07d79a2e_d4e9_0bbb_be90_936274444c8c
  4700235e_e898_7ba3_34c3_9e283b222d97["contextvars"]
  e41999b9_ee75_d78c_cb43_68601b39ae25 --> 4700235e_e898_7ba3_34c3_9e283b222d97
  0dda2280_3359_8460_301c_e98c77e78185["typing"]
  e41999b9_ee75_d78c_cb43_68601b39ae25 --> 0dda2280_3359_8460_301c_e98c77e78185
  534f6e44_61b8_3c38_8b89_6934a6df9802["__init__.py"]
  e41999b9_ee75_d78c_cb43_68601b39ae25 --> 534f6e44_61b8_3c38_8b89_6934a6df9802
  a7c04dee_ee23_5891_b185_47ff6bed036d["testclient.py"]
  e41999b9_ee75_d78c_cb43_68601b39ae25 --> a7c04dee_ee23_5891_b185_47ff6bed036d
  style e41999b9_ee75_d78c_cb43_68601b39ae25 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

from collections.abc import Awaitable
from contextvars import ContextVar
from typing import Any, Callable, Optional

from fastapi import Depends, FastAPI, Request, Response
from fastapi.testclient import TestClient

legacy_request_state_context_var: ContextVar[Optional[dict[str, Any]]] = ContextVar(
    "legacy_request_state_context_var", default=None
)

app = FastAPI()


async def set_up_request_state_dependency():
    request_state = {"user": "deadpond"}
    contextvar_token = legacy_request_state_context_var.set(request_state)
    yield request_state
    legacy_request_state_context_var.reset(contextvar_token)


@app.middleware("http")
async def custom_middleware(
    request: Request, call_next: Callable[[Request], Awaitable[Response]]
):
    response = await call_next(request)
    response.headers["custom"] = "foo"
    return response


@app.get("/user", dependencies=[Depends(set_up_request_state_dependency)])
def get_user():
    request_state = legacy_request_state_context_var.get()
    assert request_state
    return request_state["user"]


client = TestClient(app)


def test_dependency_contextvars():
    """
    Check that custom middlewares don't affect the contextvar context for dependencies.

    The code before yield and the code after yield should be run in the same contextvar
    context, so that request_state_context_var.reset(contextvar_token).

    If they are run in a different context, that raises an error.
    """
    response = client.get("/user")
    assert response.json() == "deadpond"
    assert response.headers["custom"] == "foo"

Domain

Subdomains

Dependencies

Frequently Asked Questions

What does test_dependency_contextvars.py do?
test_dependency_contextvars.py is a source file in the fastapi codebase, written in python. It belongs to the FastAPI domain, Responses subdomain.
What functions are defined in test_dependency_contextvars.py?
test_dependency_contextvars.py defines 4 function(s): custom_middleware, get_user, set_up_request_state_dependency, test_dependency_contextvars.
What does test_dependency_contextvars.py depend on?
test_dependency_contextvars.py imports 5 module(s): __init__.py, collections.abc, contextvars, testclient.py, typing.
Where is test_dependency_contextvars.py in the architecture?
test_dependency_contextvars.py is located at tests/test_dependency_contextvars.py (domain: FastAPI, subdomain: Responses, directory: tests).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free