Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨Add support for PEP-593 Annotated for specifying dependencies and parameters #4871

Merged
merged 19 commits into from Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions docs_src/annotated/tutorial001.py
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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}