mirror of
https://github.com/fastapi/fastapi.git
synced 2026-01-23 05:19:46 -05:00
Compare commits
1 Commits
Update-emb
...
fix-instal
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9d2eb2b4dc |
18
README.md
18
README.md
@@ -164,6 +164,8 @@ $ 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()
|
||||||
@@ -175,7 +177,7 @@ def read_root():
|
|||||||
|
|
||||||
|
|
||||||
@app.get("/items/{item_id}")
|
@app.get("/items/{item_id}")
|
||||||
def read_item(item_id: int, q: str | None = None):
|
def read_item(item_id: int, q: Union[str, None] = None):
|
||||||
return {"item_id": item_id, "q": q}
|
return {"item_id": item_id, "q": q}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -184,7 +186,9 @@ def read_item(item_id: int, q: str | None = None):
|
|||||||
|
|
||||||
If your code uses `async` / `await`, use `async def`:
|
If your code uses `async` / `await`, use `async def`:
|
||||||
|
|
||||||
```Python hl_lines="7 12"
|
```Python hl_lines="9 14"
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
@@ -196,7 +200,7 @@ async def read_root():
|
|||||||
|
|
||||||
|
|
||||||
@app.get("/items/{item_id}")
|
@app.get("/items/{item_id}")
|
||||||
async def read_item(item_id: int, q: str | None = None):
|
async def read_item(item_id: int, q: Union[str, None] = None):
|
||||||
return {"item_id": item_id, "q": q}
|
return {"item_id": item_id, "q": q}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -287,7 +291,9 @@ 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="2 7-10 23-25"
|
```Python hl_lines="4 9-12 25-27"
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
@@ -297,7 +303,7 @@ app = FastAPI()
|
|||||||
class Item(BaseModel):
|
class Item(BaseModel):
|
||||||
name: str
|
name: str
|
||||||
price: float
|
price: float
|
||||||
is_offer: bool | None = None
|
is_offer: Union[bool, None] = None
|
||||||
|
|
||||||
|
|
||||||
@app.get("/")
|
@app.get("/")
|
||||||
@@ -306,7 +312,7 @@ def read_root():
|
|||||||
|
|
||||||
|
|
||||||
@app.get("/items/{item_id}")
|
@app.get("/items/{item_id}")
|
||||||
def read_item(item_id: int, q: str | None = None):
|
def read_item(item_id: int, q: Union[str, None] = None):
|
||||||
return {"item_id": item_id, "q": q}
|
return {"item_id": item_id, "q": q}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ Create a virtual environment and install the required packages with <a href="htt
|
|||||||
<div class="termy">
|
<div class="termy">
|
||||||
|
|
||||||
```console
|
```console
|
||||||
$ uv sync
|
$ uv sync --extra all
|
||||||
|
|
||||||
---> 100%
|
---> 100%
|
||||||
```
|
```
|
||||||
@@ -32,9 +32,9 @@ That way, you don't have to "install" your local version to be able to test ever
|
|||||||
|
|
||||||
/// note | Technical Details
|
/// note | Technical Details
|
||||||
|
|
||||||
This only happens when you install using `uv sync` instead of running `pip install fastapi` directly.
|
This only happens when you install using `uv sync --extra all` instead of running `pip install fastapi` directly.
|
||||||
|
|
||||||
That is because `uv sync` will install the local version of FastAPI in "editable" mode by default.
|
That is because `uv sync --extra all` will install the local version of FastAPI in "editable" mode by default.
|
||||||
|
|
||||||
///
|
///
|
||||||
|
|
||||||
|
|||||||
@@ -145,6 +145,8 @@ 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()
|
||||||
@@ -156,7 +158,7 @@ def read_root():
|
|||||||
|
|
||||||
|
|
||||||
@app.get("/items/{item_id}")
|
@app.get("/items/{item_id}")
|
||||||
def read_item(item_id: int, q: str | None = None):
|
def read_item(item_id: int, q: Union[str, None] = None):
|
||||||
return {"item_id": item_id, "q": q}
|
return {"item_id": item_id, "q": q}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -161,6 +161,8 @@ $ 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()
|
||||||
@@ -172,7 +174,7 @@ def read_root():
|
|||||||
|
|
||||||
|
|
||||||
@app.get("/items/{item_id}")
|
@app.get("/items/{item_id}")
|
||||||
def read_item(item_id: int, q: str | None = None):
|
def read_item(item_id: int, q: Union[str, None] = None):
|
||||||
return {"item_id": item_id, "q": q}
|
return {"item_id": item_id, "q": q}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -181,7 +183,9 @@ def read_item(item_id: int, q: str | None = None):
|
|||||||
|
|
||||||
If your code uses `async` / `await`, use `async def`:
|
If your code uses `async` / `await`, use `async def`:
|
||||||
|
|
||||||
```Python hl_lines="7 12"
|
```Python hl_lines="9 14"
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
@@ -193,7 +197,7 @@ async def read_root():
|
|||||||
|
|
||||||
|
|
||||||
@app.get("/items/{item_id}")
|
@app.get("/items/{item_id}")
|
||||||
async def read_item(item_id: int, q: str | None = None):
|
async def read_item(item_id: int, q: Union[str, None] = None):
|
||||||
return {"item_id": item_id, "q": q}
|
return {"item_id": item_id, "q": q}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -284,7 +288,9 @@ 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="2 7-10 23-25"
|
```Python hl_lines="4 9-12 25-27"
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
@@ -294,7 +300,7 @@ app = FastAPI()
|
|||||||
class Item(BaseModel):
|
class Item(BaseModel):
|
||||||
name: str
|
name: str
|
||||||
price: float
|
price: float
|
||||||
is_offer: bool | None = None
|
is_offer: Union[bool, None] = None
|
||||||
|
|
||||||
|
|
||||||
@app.get("/")
|
@app.get("/")
|
||||||
@@ -303,7 +309,7 @@ def read_root():
|
|||||||
|
|
||||||
|
|
||||||
@app.get("/items/{item_id}")
|
@app.get("/items/{item_id}")
|
||||||
def read_item(item_id: int, q: str | None = None):
|
def read_item(item_id: int, q: Union[str, None] = None):
|
||||||
return {"item_id": item_id, "q": q}
|
return {"item_id": item_id, "q": q}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -102,16 +102,15 @@ 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