docs: update required but nullable request body example, remove py39 files, add test_tutorial005

This commit is contained in:
Alberto Zambrano 2026-02-13 16:56:12 +01:00
parent 2f17a7a79a
commit a3aa8583d8
4 changed files with 8 additions and 11 deletions

View File

@ -62,15 +62,11 @@ In Python type hints, a parameter can be **required** and still allow the value
This means that the field must be present in the request body, but its value can be `null`
(`None` in Python).
This typically happens when you use `Optional[T]` **without** providing a default value.
This typically happens when you use something like `str | None` (or `Optional[str]`) **without** providing a default value.
For example:
{* ../../docs_src/body/tutorial005_py39.py hl[6] *}
And for Python 3.10+:
{* ../../docs_src/body/tutorial005_py310.py hl[6] *}
{* ../../docs_src/body/tutorial005_py310.py hl[8] *}
In this example:

View File

@ -5,7 +5,7 @@ app = FastAPI()
class Item(BaseModel):
description: str | None
description: str | None = None
@app.post("/items/")

View File

@ -3,16 +3,15 @@ import importlib
import pytest
from fastapi.testclient import TestClient
from ...utils import needs_py310
@pytest.fixture(
name="client",
params=[
pytest.param("tutorial005_py39"),
pytest.param("tutorial005_py310", marks=needs_py310),
pytest.param("tutorial005_py310"),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.body.{request.param}")
client = TestClient(mod.app)
@ -27,4 +26,6 @@ def test_required_nullable_field(client: TestClient):
def test_required_field_missing(client: TestClient):
response = client.post("/items/", json={})
assert response.status_code == 422
assert response.status_code == 200
assert response.json() == {"description": None}