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. 16
      fastapi/routing.py
  2. 3
      tests/test_issue_11215.py
  3. 7
      tests/test_issue_11215_coverage.py

16
fastapi/routing.py

@ -401,21 +401,25 @@ def get_request_handler(
# Create a new BackgroundTasks to hold both # Create a new BackgroundTasks to hold both
combined_tasks = BackgroundTasks() combined_tasks = BackgroundTasks()
# Add existing response tasks # Add existing response tasks
if isinstance(raw_response.background, BackgroundTasks): if isinstance(raw_response.background, BackgroundTasks):
combined_tasks.tasks.extend(raw_response.background.tasks) combined_tasks.tasks.extend(raw_response.background.tasks)
elif isinstance(raw_response.background, BackgroundTask): elif isinstance(raw_response.background, BackgroundTask):
combined_tasks.tasks.append(raw_response.background) combined_tasks.tasks.append(raw_response.background)
# Add dependency tasks # Add dependency tasks
# solved_result.background_tasks is always BackgroundTasks (from dependencies/utils.py) # solved_result.background_tasks is always BackgroundTasks (from dependencies/utils.py)
if isinstance(solved_result.background_tasks, BackgroundTasks): if isinstance(solved_result.background_tasks, BackgroundTasks):
combined_tasks.tasks.extend(solved_result.background_tasks.tasks) combined_tasks.tasks.extend(
elif isinstance(solved_result.background_tasks, BackgroundTask): # pragma: no cover solved_result.background_tasks.tasks
# Should not happen for BackgroundTasks dependency but safe to handle )
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) combined_tasks.tasks.append(solved_result.background_tasks)
raw_response.background = combined_tasks raw_response.background = combined_tasks
response = raw_response response = raw_response
else: else:

3
tests/test_issue_11215.py

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

7
tests/test_issue_11215_coverage.py

@ -5,20 +5,23 @@ from starlette.responses import Response
app = FastAPI() app = FastAPI()
@app.get("/") @app.get("/")
def endpoint(tasks: BackgroundTasks): def endpoint(tasks: BackgroundTasks):
tasks.add_task(lambda: print("Dependency task")) tasks.add_task(lambda: print("Dependency task"))
response_tasks = StarletteBackgroundTasks() response_tasks = StarletteBackgroundTasks()
response_tasks.add_task(lambda: print("Response task")) response_tasks.add_task(lambda: print("Response task"))
return Response( return Response(
content="Custom response", content="Custom response",
background=response_tasks, background=response_tasks,
) )
client = TestClient(app) client = TestClient(app)
def test_issue_11215_response_background_tasks_collection(capsys): def test_issue_11215_response_background_tasks_collection(capsys):
client.get("/") client.get("/")
captured = capsys.readouterr() captured = capsys.readouterr()

Loading…
Cancel
Save