Skip to content

Commit

Permalink
✨Add support for PEP-593 Annotated for specifying dependencies and …
Browse files Browse the repository at this point in the history
…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 <[email protected]>
  • Loading branch information
3 people authored Mar 17, 2023
1 parent ef176c6 commit 375513f
Show file tree
Hide file tree
Showing 24 changed files with 1,293 additions and 156 deletions.
18 changes: 18 additions & 0 deletions docs_src/annotated/tutorial001.py
Original file line number Diff line number Diff line change
@@ -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 changes: 17 additions & 0 deletions docs_src/annotated/tutorial001_py39.py
Original file line number Diff line number Diff line change
@@ -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 changes: 21 additions & 0 deletions docs_src/annotated/tutorial002.py
Original file line number Diff line number Diff line change
@@ -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 changes: 20 additions & 0 deletions docs_src/annotated/tutorial002_py39.py
Original file line number Diff line number Diff line change
@@ -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 changes: 15 additions & 0 deletions docs_src/annotated/tutorial003.py
Original file line number Diff line number Diff line change
@@ -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 changes: 16 additions & 0 deletions docs_src/annotated/tutorial003_py39.py
Original file line number Diff line number Diff line change
@@ -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}
Loading

0 comments on commit 375513f

Please sign in to comment.