From a28557e0c1d022427291ac8a2a5712b1b663a75f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 10 Nov 2025 09:25:49 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20[pre-commit.ci]=20Auto=20format?= =?UTF-8?q?=20from=20pre-commit.com=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/en/docs/tutorial/query-params-str-validations.md | 2 +- .../query_params_str_validations/tutorial006d_an_py310.py | 4 +++- .../test_tutorial006d_an_py310.py | 2 ++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/docs/en/docs/tutorial/query-params-str-validations.md b/docs/en/docs/tutorial/query-params-str-validations.md index 68e9a3d5f9..e24c248ac3 100644 --- a/docs/en/docs/tutorial/query-params-str-validations.md +++ b/docs/en/docs/tutorial/query-params-str-validations.md @@ -257,7 +257,7 @@ For example, you might try: q: Annotated[str | None, Query(min_length=3)] = ... ``` -But this will still expect a **string** value, and if the client omits `q` or tries to send `q=None`, FastAPI will raise a validation error. +But this will still expect a **string** value, and if the client omits `q` or tries to send `q=None`, FastAPI will raise a validation error. In other words, `None` is not an acceptable runtime value for query parameters — only strings are. If you want to accept special values (like `"None"` or an empty string) and interpret them as `None` in your application, you can handle them manually in your function: diff --git a/docs_src/query_params_str_validations/tutorial006d_an_py310.py b/docs_src/query_params_str_validations/tutorial006d_an_py310.py index 0962fbce7e..025d314ea2 100644 --- a/docs_src/query_params_str_validations/tutorial006d_an_py310.py +++ b/docs_src/query_params_str_validations/tutorial006d_an_py310.py @@ -1,8 +1,10 @@ -from fastapi import FastAPI, Query from typing import Annotated +from fastapi import FastAPI, Query + app = FastAPI() + @app.get("/items/") async def read_items(q: Annotated[str | None, Query()] = ...): if q in ("None", "", "null"): diff --git a/tests/test_tutorial/test_query_params_str_validations/test_tutorial006d_an_py310.py b/tests/test_tutorial/test_query_params_str_validations/test_tutorial006d_an_py310.py index 57fccec947..36ae95f65c 100644 --- a/tests/test_tutorial/test_query_params_str_validations/test_tutorial006d_an_py310.py +++ b/tests/test_tutorial/test_query_params_str_validations/test_tutorial006d_an_py310.py @@ -1,8 +1,10 @@ from fastapi.testclient import TestClient + from docs_src.query_params_str_validations.tutorial006d_an_py310 import app client = TestClient(app) + def test_read_items(): response = client.get("/items/", params={"q": "None"}) assert response.status_code == 200