Skip to content

Commit b239978

Browse files
committed
Make WireupTask a regular dependency
1 parent 96116fb commit b239978

5 files changed

Lines changed: 66 additions & 0 deletions

File tree

docs/pages/integrations/fastapi/background_tasks.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Use `WireupTask` when you want Wireup to resolve dependencies inside a FastAPI b
44

55
## Usage
66

7+
1. Add `wireup.integration.fastapi` to your Wireup injectables.
78
1. Define your task function with `Injected[...]` parameters for any Wireup dependencies
89
1. Inject `WireupTask` in your route handler: `wireup_task: Injected[WireupTask]`
910
1. Wrap the task function before scheduling: `wireup_task(write_greeting)`

docs/pages/integrations/starlette/background_tasks.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Use `WireupTask` when you want Wireup to resolve dependencies inside a Starlette
44

55
## Usage
66

7+
1. Add `wireup.integration.starlette` to your Wireup injectables.
78
1. Define your task function with `Injected[...]` parameters for any Wireup dependencies
89
1. Inject `WireupTask` in your route handler: `wireup_task: Injected[WireupTask]`
910
1. Wrap the task function before scheduling: `wireup_task(write_greeting)`

test/integration/fastapi/test_fastapi_integration.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,29 @@ async def hello(tasks: BackgroundTasks, wireup_task: Injected[WireupTask]) -> Di
600600
assert task_result == ["fastapi:4"]
601601

602602

603+
def test_setup_still_exposes_wireup_task_without_integration_module_registration() -> None:
604+
task_result: list[str] = []
605+
606+
def write_logs(name: str, random_service: Injected[RandomService]) -> None:
607+
task_result.append(f"{name}:{random_service.get_random()}")
608+
609+
app = FastAPI()
610+
container = wireup.create_async_container(injectables=[shared_services])
611+
612+
@app.get("/")
613+
async def hello(tasks: BackgroundTasks, wireup_task: Injected[WireupTask]) -> Dict[str, Any]:
614+
tasks.add_task(wireup_task(write_logs), "fallback")
615+
return {}
616+
617+
wireup.integration.fastapi.setup(container, app)
618+
619+
with TestClient(app) as client:
620+
response = client.get("/")
621+
622+
assert response.status_code == 200
623+
assert task_result == ["fallback:4"]
624+
625+
603626
def test_background_task_uses_different_scope_than_request() -> None:
604627
ids: dict[str, str] = {}
605628

test/integration/starlette/test_starlette_integration.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,42 @@ async def hello_with_background_task(
236236
assert task_result == ["Hello Async"]
237237

238238

239+
async def test_wireup_task_is_resolvable_from_integration_module_registration() -> None:
240+
container = wireup.create_async_container(injectables=[shared_services, wireup.integration.starlette])
241+
242+
task = await container.get(WireupTask)
243+
244+
assert isinstance(task, WireupTask)
245+
assert task.container is container
246+
247+
248+
def test_setup_still_exposes_wireup_task_without_integration_module_registration() -> None:
249+
task_result: list[str] = []
250+
251+
def write_logs(name: str, greeter: Injected[GreeterService]) -> None:
252+
task_result.append(greeter.greet(name))
253+
254+
@inject
255+
async def hello_with_background_task(
256+
_request: Request,
257+
wireup_task: Injected[WireupTask],
258+
) -> PlainTextResponse:
259+
return PlainTextResponse(
260+
"ok",
261+
background=BackgroundTask(wireup_task(write_logs), "Fallback"),
262+
)
263+
264+
app = Starlette(routes=[Route("/hello_bg", hello_with_background_task, methods=["GET"])])
265+
container = wireup.create_async_container(injectables=[shared_services])
266+
wireup.integration.starlette.setup(container, app)
267+
268+
with TestClient(app) as client:
269+
response = client.get("/hello_bg")
270+
271+
assert response.status_code == 200
272+
assert task_result == ["Hello Fallback"]
273+
274+
239275
def test_background_task_uses_different_scope_than_request() -> None:
240276
ids: dict[str, str] = {}
241277

wireup/integration/starlette.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ def get_request_container() -> ScopedAsyncContainer:
114114
raise WireupError(msg) from e
115115

116116

117+
@injectable
117118
class WireupTask:
118119
__slots__ = ("_get_injected_wrapper", "container")
119120

@@ -138,6 +139,10 @@ def _expose_wireup_task(container: AsyncContainer) -> None:
138139
if container._registry.is_type_with_qualifier_known(WireupTask, None):
139140
return
140141

142+
# Keep the old behavior for WireupTask where it was exposed
143+
# without adding the integration in injectables.
144+
# Cannot properly raise a deprecation notice here but this is deprecated
145+
# and marked for removal.
141146
def wireup_task_factory() -> WireupTask:
142147
return WireupTask(container)
143148

0 commit comments

Comments
 (0)