|
|
|
@ -161,8 +161,6 @@ $ pip install "fastapi[standard]" |
|
|
|
Создайте файл `main.py` со следующим содержимым: |
|
|
|
|
|
|
|
```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): |
|
|
|
|
|
|
|
Если ваш код использует `async` / `await`, используйте `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 @@ INFO: Application startup complete. |
|
|
|
|
|
|
|
Объявите тело запроса, используя стандартные типы Python, спасибо 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} |
|
|
|
|
|
|
|
|
|
|
|
|