-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨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 <[email protected]>
- Loading branch information
1 parent
ef176c6
commit 375513f
Showing
24 changed files
with
1,293 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
Oops, something went wrong.