mirror of
https://github.com/fastapi/fastapi.git
synced 2026-05-18 13:27:45 -04:00
✨Add support for PEP-593 Annotated for specifying dependencies and parameters (#4871)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
This commit is contained in:
18
docs_src/annotated/tutorial001.py
Normal file
18
docs_src/annotated/tutorial001.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from typing import Optional
|
||||
|
||||
from fastapi import Depends, FastAPI
|
||||
from typing_extensions import Annotated
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
async def common_parameters(q: Optional[str] = None, skip: int = 0, limit: int = 100):
|
||||
return {"q": q, "skip": skip, "limit": limit}
|
||||
|
||||
|
||||
CommonParamsDepends = Annotated[dict, Depends(common_parameters)]
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(commons: CommonParamsDepends):
|
||||
return commons
|
||||
17
docs_src/annotated/tutorial001_py39.py
Normal file
17
docs_src/annotated/tutorial001_py39.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from typing import Annotated, Optional
|
||||
|
||||
from fastapi import Depends, FastAPI
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
async def common_parameters(q: Optional[str] = None, skip: int = 0, limit: int = 100):
|
||||
return {"q": q, "skip": skip, "limit": limit}
|
||||
|
||||
|
||||
CommonParamsDepends = Annotated[dict, Depends(common_parameters)]
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(commons: CommonParamsDepends):
|
||||
return commons
|
||||
21
docs_src/annotated/tutorial002.py
Normal file
21
docs_src/annotated/tutorial002.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from typing import Optional
|
||||
|
||||
from fastapi import Depends, FastAPI
|
||||
from typing_extensions import Annotated
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
class CommonQueryParams:
|
||||
def __init__(self, q: Optional[str] = None, skip: int = 0, limit: int = 100):
|
||||
self.q = q
|
||||
self.skip = skip
|
||||
self.limit = limit
|
||||
|
||||
|
||||
CommonQueryParamsDepends = Annotated[CommonQueryParams, Depends()]
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(commons: CommonQueryParamsDepends):
|
||||
return commons
|
||||
20
docs_src/annotated/tutorial002_py39.py
Normal file
20
docs_src/annotated/tutorial002_py39.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from typing import Annotated, Optional
|
||||
|
||||
from fastapi import Depends, FastAPI
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
class CommonQueryParams:
|
||||
def __init__(self, q: Optional[str] = None, skip: int = 0, limit: int = 100):
|
||||
self.q = q
|
||||
self.skip = skip
|
||||
self.limit = limit
|
||||
|
||||
|
||||
CommonQueryParamsDepends = Annotated[CommonQueryParams, Depends()]
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(commons: CommonQueryParamsDepends):
|
||||
return commons
|
||||
15
docs_src/annotated/tutorial003.py
Normal file
15
docs_src/annotated/tutorial003.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from fastapi import FastAPI, Path
|
||||
from fastapi.param_functions import Query
|
||||
from typing_extensions import Annotated
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/items/{item_id}")
|
||||
async def read_items(item_id: Annotated[int, Path(gt=0)]):
|
||||
return {"item_id": item_id}
|
||||
|
||||
|
||||
@app.get("/users")
|
||||
async def read_users(user_id: Annotated[str, Query(min_length=1)] = "me"):
|
||||
return {"user_id": user_id}
|
||||
16
docs_src/annotated/tutorial003_py39.py
Normal file
16
docs_src/annotated/tutorial003_py39.py
Normal file
@@ -0,0 +1,16 @@
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import FastAPI, Path
|
||||
from fastapi.param_functions import Query
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/items/{item_id}")
|
||||
async def read_items(item_id: Annotated[int, Path(gt=0)]):
|
||||
return {"item_id": item_id}
|
||||
|
||||
|
||||
@app.get("/users")
|
||||
async def read_users(user_id: Annotated[str, Query(min_length=1)] = "me"):
|
||||
return {"user_id": user_id}
|
||||
Reference in New Issue
Block a user