@@ -45,9 +45,7 @@ def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
4545
4646async_bridge = AsyncBridge ()
4747
48- # ContextVariables
49- # These are scoped to a specific request
50- # Defining them here for now to keep things close together
48+ ################### ContextVariables ###################
5149
5250
5351@dataclass (frozen = True )
@@ -67,12 +65,9 @@ class RequestContextVars:
6765site : ContextVar [Site ] = ContextVar ("site" )
6866
6967
70- def set_context_for_sync_request () :
68+ def set_context_from_legacy_web_py () -> None :
7169 """
72- Sets the contextvars for the current request when called from web.py
73-
74- create_site is ultimately temporary as we'll need an async version of site
75- That version should be usable without instantiating a site
70+ Extracts context from the global web.ctx (sync) and populates ContextVars.
7671 """
7772 site .set (create_site ())
7873 req_context .set (
@@ -83,15 +78,12 @@ def set_context_for_sync_request():
8378 )
8479
8580
86- def set_context_for_async_request (request : Request ):
81+ def set_context_from_fastapi (request : Request ) -> None :
8782 """
88- These are set automatically in the middleware of fastapi
89- They are made available to all requests
90-
91- BEFORE ADDING:
92- Please consider if we could feasibly just pass it down to the function instead
93- This is preferable for testable and maintainable code.
83+ Extracts context from a FastAPI request (async) and populates ContextVars.
84+ Should be called within the middleware stack.
9485 """
86+ # NOTE: Avoid adding new fields here if they can be passed as function arguments instead.
9587 site .set (create_site ())
9688 req_context .set (
9789 RequestContextVars (
0 commit comments