]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
📝 Update embedded code examples to Python 3.10 syntax (#14758)
authorMotov Yurii <109919500+YuriiMotov@users.noreply.github.com>
Wed, 4 Feb 2026 12:33:07 +0000 (15:33 +0300)
committerGitHub <noreply@github.com>
Wed, 4 Feb 2026 12:33:07 +0000 (13:33 +0100)
README.md
docs/en/docs/deployment/docker.md
docs/en/docs/index.md
docs/en/docs/tutorial/body-multiple-params.md

index 1057b86942d73a67c0087ad4b88151ca766d15a1..963de51edf98fcdb4aef937697fcce0d623daed6 100644 (file)
--- a/README.md
+++ b/README.md
@@ -164,8 +164,6 @@ $ pip install "fastapi[standard]"
 Create a file `main.py` with:
 
 ```Python
-from typing import Union
-
 from fastapi import FastAPI
 
 app = FastAPI()
@@ -177,7 +175,7 @@ def read_root():
 
 
 @app.get("/items/{item_id}")
-def read_item(item_id: int, q: Union[str, None] = None):
+def read_item(item_id: int, q: str | None = None):
     return {"item_id": item_id, "q": q}
 ```
 
@@ -186,9 +184,7 @@ def read_item(item_id: int, q: Union[str, None] = None):
 
 If your code uses `async` / `await`, use `async def`:
 
-```Python hl_lines="9  14"
-from typing import Union
-
+```Python hl_lines="7  12"
 from fastapi import FastAPI
 
 app = FastAPI()
@@ -200,7 +196,7 @@ async def read_root():
 
 
 @app.get("/items/{item_id}")
-async def read_item(item_id: int, q: Union[str, None] = None):
+async def read_item(item_id: int, q: str | None = None):
     return {"item_id": item_id, "q": q}
 ```
 
@@ -291,9 +287,7 @@ Now modify the file `main.py` to receive a body from a `PUT` request.
 
 Declare the body using standard Python types, thanks to Pydantic.
 
-```Python hl_lines="4  9-12  25-27"
-from typing import Union
-
+```Python hl_lines="2  7-10 23-25"
 from fastapi import FastAPI
 from pydantic import BaseModel
 
@@ -303,7 +297,7 @@ app = FastAPI()
 class Item(BaseModel):
     name: str
     price: float
-    is_offer: Union[bool, None] = None
+    is_offer: bool | None = None
 
 
 @app.get("/")
@@ -312,7 +306,7 @@ def read_root():
 
 
 @app.get("/items/{item_id}")
-def read_item(item_id: int, q: Union[str, None] = None):
+def read_item(item_id: int, q: str | None = None):
     return {"item_id": item_id, "q": q}
 
 
index 6b71f7360d996f823da69e51e282081482f3b0bf..7219f3afcafe1b4d270a93dc04db2ec771986fda 100644 (file)
@@ -145,8 +145,6 @@ There are other formats and tools to define and install package dependencies.
 * Create a `main.py` file with:
 
 ```Python
-from typing import Union
-
 from fastapi import FastAPI
 
 app = FastAPI()
@@ -158,7 +156,7 @@ def read_root():
 
 
 @app.get("/items/{item_id}")
-def read_item(item_id: int, q: Union[str, None] = None):
+def read_item(item_id: int, q: str | None = None):
     return {"item_id": item_id, "q": q}
 ```
 
index 5eb47c7b32f25b66ae92ce4ea5831922657c4a00..ff0f4e9d9ddedd34cbc46738101aa10069aa6707 100644 (file)
@@ -161,8 +161,6 @@ $ pip install "fastapi[standard]"
 Create a file `main.py` with:
 
 ```Python
-from typing import Union
-
 from fastapi import FastAPI
 
 app = FastAPI()
@@ -174,7 +172,7 @@ def read_root():
 
 
 @app.get("/items/{item_id}")
-def read_item(item_id: int, q: Union[str, None] = None):
+def read_item(item_id: int, q: str | None = None):
     return {"item_id": item_id, "q": q}
 ```
 
@@ -183,9 +181,7 @@ def read_item(item_id: int, q: Union[str, None] = None):
 
 If your code uses `async` / `await`, use `async def`:
 
-```Python hl_lines="9  14"
-from typing import Union
-
+```Python hl_lines="7  12"
 from fastapi import FastAPI
 
 app = FastAPI()
@@ -197,7 +193,7 @@ async def read_root():
 
 
 @app.get("/items/{item_id}")
-async def read_item(item_id: int, q: Union[str, None] = None):
+async def read_item(item_id: int, q: str | None = None):
     return {"item_id": item_id, "q": q}
 ```
 
@@ -288,9 +284,7 @@ Now modify the file `main.py` to receive a body from a `PUT` request.
 
 Declare the body using standard Python types, thanks to Pydantic.
 
-```Python hl_lines="4  9-12  25-27"
-from typing import Union
-
+```Python hl_lines="2  7-10 23-25"
 from fastapi import FastAPI
 from pydantic import BaseModel
 
@@ -300,7 +294,7 @@ app = FastAPI()
 class Item(BaseModel):
     name: str
     price: float
-    is_offer: Union[bool, None] = None
+    is_offer: bool | None = None
 
 
 @app.get("/")
@@ -309,7 +303,7 @@ def read_root():
 
 
 @app.get("/items/{item_id}")
-def read_item(item_id: int, q: Union[str, None] = None):
+def read_item(item_id: int, q: str | None = None):
     return {"item_id": item_id, "q": q}
 
 
index ed23c814900e2fe378e09c92be1edad244862029..bb0c583685579d525fb869d98225add06fa7d1bc 100644 (file)
@@ -103,15 +103,16 @@ Of course, you can also declare additional query parameters whenever you need, a
 As, by default, singular values are interpreted as query parameters, you don't have to explicitly add a `Query`, you can just do:
 
 ```Python
-q: Union[str, None] = None
+q: str | None = None
 ```
 
-Or in Python 3.10 and above:
+Or in Python 3.9:
 
 ```Python
-q: str | None = None
+q: Union[str, None] = None
 ```
 
+
 For example:
 
 {* ../../docs_src/body_multiple_params/tutorial004_an_py310.py hl[28] *}