Allow `None` as return type for bodiless responses (#9425)

Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
This commit is contained in:
Robert Hofer 2025-09-20 20:44:43 +02:00 committed by GitHub
parent 0bdc3ca373
commit b51ec36f2e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 0 deletions

View File

@ -254,6 +254,8 @@ def get_typed_annotation(annotation: Any, globalns: Dict[str, Any]) -> Any:
if isinstance(annotation, str): if isinstance(annotation, str):
annotation = ForwardRef(annotation) annotation = ForwardRef(annotation)
annotation = evaluate_forwardref(annotation, globalns, globalns) annotation = evaluate_forwardref(annotation, globalns, globalns)
if annotation is type(None):
return None
return annotation return annotation

View File

@ -0,0 +1,17 @@
import http
from fastapi import FastAPI
from fastapi.testclient import TestClient
def test_no_content():
app = FastAPI()
@app.get("/no-content", status_code=http.HTTPStatus.NO_CONTENT)
def return_no_content() -> "None":
return
client = TestClient(app)
response = client.get("/no-content")
assert response.status_code == http.HTTPStatus.NO_CONTENT, response.text
assert not response.content