Browse Source

Update code examples in `docs/index.md` to follow latest Python syntax

pull/14758/head
Yurii Motov 6 months ago
parent
commit
430529be0b
  1. 18
      docs/en/docs/index.md

18
docs/en/docs/index.md

@ -161,8 +161,6 @@ $ pip install "fastapi[standard]"
Create a file `main.py` with: Create a file `main.py` with:
```Python ```Python
from typing import Union
from fastapi import FastAPI from fastapi import FastAPI
app = FastAPI() app = FastAPI()
@ -174,7 +172,7 @@ def read_root():
@app.get("/items/{item_id}") @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} 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`: If your code uses `async` / `await`, use `async def`:
```Python hl_lines="9 14" ```Python hl_lines="7 12"
from typing import Union
from fastapi import FastAPI from fastapi import FastAPI
app = FastAPI() app = FastAPI()
@ -197,7 +193,7 @@ async def read_root():
@app.get("/items/{item_id}") @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} 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. Declare the body using standard Python types, thanks to Pydantic.
```Python hl_lines="4 9-12 25-27" ```Python hl_lines="2 7-10 23-25"
from typing import Union
from fastapi import FastAPI from fastapi import FastAPI
from pydantic import BaseModel from pydantic import BaseModel
@ -300,7 +294,7 @@ app = FastAPI()
class Item(BaseModel): class Item(BaseModel):
name: str name: str
price: float price: float
is_offer: Union[bool, None] = None is_offer: bool | None = None
@app.get("/") @app.get("/")
@ -309,7 +303,7 @@ def read_root():
@app.get("/items/{item_id}") @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} return {"item_id": item_id, "q": q}

Loading…
Cancel
Save