mirror of
https://github.com/fastapi/fastapi.git
synced 2026-01-23 13:30:01 -05:00
Compare commits
4 Commits
master
...
Update-emb
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6308c9416e | ||
|
|
d346026e17 | ||
|
|
7256d3333d | ||
|
|
430529be0b |
1
.github/labeler.yml
vendored
1
.github/labeler.yml
vendored
@@ -31,7 +31,6 @@ internal:
|
|||||||
- .pre-commit-config.yaml
|
- .pre-commit-config.yaml
|
||||||
- pdm_build.py
|
- pdm_build.py
|
||||||
- requirements*.txt
|
- requirements*.txt
|
||||||
- uv.lock
|
|
||||||
- docs/en/data/sponsors.yml
|
- docs/en/data/sponsors.yml
|
||||||
- docs/en/overrides/main.html
|
- docs/en/overrides/main.html
|
||||||
- all-globs-to-all-files:
|
- all-globs-to-all-files:
|
||||||
|
|||||||
18
README.md
18
README.md
@@ -164,8 +164,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()
|
||||||
@@ -177,7 +175,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}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -186,9 +184,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()
|
||||||
@@ -200,7 +196,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}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -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.
|
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
|
||||||
|
|
||||||
@@ -303,7 +297,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("/")
|
||||||
@@ -312,7 +306,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}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -145,8 +145,6 @@ There are other formats and tools to define and install package dependencies.
|
|||||||
* Create a `main.py` file with:
|
* Create a `main.py` file with:
|
||||||
|
|
||||||
```Python
|
```Python
|
||||||
from typing import Union
|
|
||||||
|
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
@@ -158,7 +156,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}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -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}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -36,7 +36,6 @@ hide:
|
|||||||
|
|
||||||
### Internal
|
### Internal
|
||||||
|
|
||||||
* 🔧 Ensure that an edit to `uv.lock` gets the `internal` label. PR [#14759](https://github.com/fastapi/fastapi/pull/14759) by [@svlandeg](https://github.com/svlandeg).
|
|
||||||
* 🔧 Update sponsors: remove Requestly. PR [#14735](https://github.com/fastapi/fastapi/pull/14735) by [@tiangolo](https://github.com/tiangolo).
|
* 🔧 Update sponsors: remove Requestly. PR [#14735](https://github.com/fastapi/fastapi/pull/14735) by [@tiangolo](https://github.com/tiangolo).
|
||||||
* 🔧 Update sponsors, LambdaTest changes to TestMu AI. PR [#14734](https://github.com/fastapi/fastapi/pull/14734) by [@tiangolo](https://github.com/tiangolo).
|
* 🔧 Update sponsors, LambdaTest changes to TestMu AI. PR [#14734](https://github.com/fastapi/fastapi/pull/14734) by [@tiangolo](https://github.com/tiangolo).
|
||||||
* ⬆ Bump actions/cache from 4 to 5. PR [#14511](https://github.com/fastapi/fastapi/pull/14511) by [@dependabot[bot]](https://github.com/apps/dependabot).
|
* ⬆ Bump actions/cache from 4 to 5. PR [#14511](https://github.com/fastapi/fastapi/pull/14511) by [@dependabot[bot]](https://github.com/apps/dependabot).
|
||||||
|
|||||||
@@ -102,15 +102,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:
|
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: str | None = None
|
||||||
|
```
|
||||||
|
|
||||||
|
Or in Python 3.9:
|
||||||
|
|
||||||
```Python
|
```Python
|
||||||
q: Union[str, None] = None
|
q: Union[str, None] = None
|
||||||
```
|
```
|
||||||
|
|
||||||
Or in Python 3.10 and above:
|
|
||||||
|
|
||||||
```Python
|
|
||||||
q: str | None = None
|
|
||||||
```
|
|
||||||
|
|
||||||
For example:
|
For example:
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user