Skip to content

Commit 375513f

Browse files
nzigpre-commit-ci[bot]tiangolo
authored
✨Add support for PEP-593 Annotated for specifying dependencies and parameters (fastapi#4871)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Sebastián Ramírez <[email protected]>
1 parent ef176c6 commit 375513f

24 files changed

Lines changed: 1293 additions & 156 deletions

docs_src/annotated/tutorial001.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from typing import Optional
2+
3+
from fastapi import Depends, FastAPI
4+
from typing_extensions import Annotated
5+
6+
app = FastAPI()
7+
8+
9+
async def common_parameters(q: Optional[str] = None, skip: int = 0, limit: int = 100):
10+
return {"q": q, "skip": skip, "limit": limit}
11+
12+
13+
CommonParamsDepends = Annotated[dict, Depends(common_parameters)]
14+
15+
16+
@app.get("/items/")
17+
async def read_items(commons: CommonParamsDepends):
18+
return commons
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from typing import Annotated, Optional
2+
3+
from fastapi import Depends, FastAPI
4+
5+
app = FastAPI()
6+
7+
8+
async def common_parameters(q: Optional[str] = None, skip: int = 0, limit: int = 100):
9+
return {"q": q, "skip": skip, "limit": limit}
10+
11+
12+
CommonParamsDepends = Annotated[dict, Depends(common_parameters)]
13+
14+
15+
@app.get("/items/")
16+
async def read_items(commons: CommonParamsDepends):
17+
return commons

docs_src/annotated/tutorial002.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from typing import Optional
2+
3+
from fastapi import Depends, FastAPI
4+
from typing_extensions import Annotated
5+
6+
app = FastAPI()
7+
8+
9+
class CommonQueryParams:
10+
def __init__(self, q: Optional[str] = None, skip: int = 0, limit: int = 100):
11+
self.q = q
12+
self.skip = skip
13+
self.limit = limit
14+
15+
16+
CommonQueryParamsDepends = Annotated[CommonQueryParams, Depends()]
17+
18+
19+
@app.get("/items/")
20+
async def read_items(commons: CommonQueryParamsDepends):
21+
return commons
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from typing import Annotated, Optional
2+
3+
from fastapi import Depends, FastAPI
4+
5+
app = FastAPI()
6+
7+
8+
class CommonQueryParams:
9+
def __init__(self, q: Optional[str] = None, skip: int = 0, limit: int = 100):
10+
self.q = q
11+
self.skip = skip
12+
self.limit = limit
13+
14+
15+
CommonQueryParamsDepends = Annotated[CommonQueryParams, Depends()]
16+
17+
18+
@app.get("/items/")
19+
async def read_items(commons: CommonQueryParamsDepends):
20+
return commons

docs_src/annotated/tutorial003.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from fastapi import FastAPI, Path
2+
from fastapi.param_functions import Query
3+
from typing_extensions import Annotated
4+
5+
app = FastAPI()
6+
7+
8+
@app.get("/items/{item_id}")
9+
async def read_items(item_id: Annotated[int, Path(gt=0)]):
10+
return {"item_id": item_id}
11+
12+
13+
@app.get("/users")
14+
async def read_users(user_id: Annotated[str, Query(min_length=1)] = "me"):
15+
return {"user_id": user_id}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from typing import Annotated
2+
3+
from fastapi import FastAPI, Path
4+
from fastapi.param_functions import Query
5+
6+
app = FastAPI()
7+
8+
9+
@app.get("/items/{item_id}")
10+
async def read_items(item_id: Annotated[int, Path(gt=0)]):
11+
return {"item_id": item_id}
12+
13+
14+
@app.get("/users")
15+
async def read_users(user_id: Annotated[str, Query(min_length=1)] = "me"):
16+
return {"user_id": user_id}

0 commit comments

Comments
 (0)