Browse Source

🎨 [pre-commit.ci] Auto format from pre-commit.com hooks

pull/14386/head
pre-commit-ci[bot] 8 months ago
parent
commit
d24bfcf220
  1. 10
      fastapi/routing.py
  2. 3
      tests/test_issue_11215.py
  3. 3
      tests/test_issue_11215_coverage.py

10
fastapi/routing.py

@ -411,9 +411,13 @@ def get_request_handler(
# Add dependency tasks
# solved_result.background_tasks is always BackgroundTasks (from dependencies/utils.py)
if isinstance(solved_result.background_tasks, BackgroundTasks):
combined_tasks.tasks.extend(solved_result.background_tasks.tasks)
elif isinstance(solved_result.background_tasks, BackgroundTask): # pragma: no cover
# Should not happen for BackgroundTasks dependency but safe to handle
combined_tasks.tasks.extend(
solved_result.background_tasks.tasks
)
elif isinstance(
solved_result.background_tasks, BackgroundTask
): # pragma: no cover
# Should not happen for BackgroundTasks dependency but safe to handle
combined_tasks.tasks.append(solved_result.background_tasks)
raw_response.background = combined_tasks

3
tests/test_issue_11215.py

@ -4,6 +4,7 @@ from starlette.responses import BackgroundTask, Response
app = FastAPI()
@app.get("/")
def endpoint(tasks: BackgroundTasks):
tasks.add_task(lambda: print("Dependency task executed"))
@ -12,8 +13,10 @@ def endpoint(tasks: BackgroundTasks):
background=BackgroundTask(lambda: print("Response task executed")),
)
client = TestClient(app)
def test_issue_11215(capsys):
client.get("/")
captured = capsys.readouterr()

3
tests/test_issue_11215_coverage.py

@ -5,6 +5,7 @@ from starlette.responses import Response
app = FastAPI()
@app.get("/")
def endpoint(tasks: BackgroundTasks):
tasks.add_task(lambda: print("Dependency task"))
@ -17,8 +18,10 @@ def endpoint(tasks: BackgroundTasks):
background=response_tasks,
)
client = TestClient(app)
def test_issue_11215_response_background_tasks_collection(capsys):
client.get("/")
captured = capsys.readouterr()

Loading…
Cancel
Save