FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

📝 Update embedded code examples to Python 3.10 syntax by YuriiMotov · Pull Request #14758 · fastapi/fastapi · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .md  (4) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
18 changes: 6 additions & 12 deletions README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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}
```

Expand All @@ -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()
Expand All @@ -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}
```

Expand Down Expand Up @@ -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

Expand All @@ -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("/")
Expand All @@ -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}


Expand Down
4 changes: 1 addition & 3 deletions docs/en/docs/deployment/docker.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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}
```

Expand Down
18 changes: 6 additions & 12 deletions docs/en/docs/index.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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}
```

Expand All @@ -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()
Expand All @@ -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}
```

Expand Down Expand Up @@ -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

Expand All @@ -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("/")
Expand All @@ -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}


Expand Down
7 changes: 4 additions & 3 deletions docs/en/docs/tutorial/body-multiple-params.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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] *}
Expand Down

Back | FazBrowse Home | New Git URL