First check
Example
I propse to allow transforming:
from typing import 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}
@app.get("/items/")
async def read_items(commons: dict = Depends(common_parameters)):
return commons
to
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}
@app.get("/items/")
async def read_items(commons: Annotated[dict, Depends(common_parameters)]):
return commons
Discussion
PEP 593 Added Annotated for adding additional annotations beyond type annotations. I think FastAPI's Depends, Query, Body and the likes fit well with the kind of additional annotations this supports.
This would also make default values less awkward:
@app.get("/items/")
async def read_items(q: Optional[str] = Query(None, max_length=50)):
pass
Could become
@app.get("/items/")
async def read_items(q: Annotated[Optional[str], Query(max_length=50)] = None):
pass
This will also solve the issue mentioned in the docs of parameter ordering.
Finally, it is sometimes convenient to use the same function as both a FastAPI dependency and a regular function. In these cases, because = Depends(...) is a default parameter value, if you forget to pass a parameter the error is not caught by your IDE. Worse, it is not caught at runtime because Python will just pass along the Depends object. This will probably cause an error down the road, but may silently succeed in some cases.
I'm willing to implement this if you think it's a good idea.
First check
Example
I propse to allow transforming:
to
Discussion
PEP 593 Added
Annotatedfor adding additional annotations beyond type annotations. I think FastAPI'sDepends,Query,Bodyand the likes fit well with the kind of additional annotations this supports.This would also make default values less awkward:
Could become
This will also solve the issue mentioned in the docs of parameter ordering.
Finally, it is sometimes convenient to use the same function as both a FastAPI dependency and a regular function. In these cases, because
= Depends(...)is a default parameter value, if you forget to pass a parameter the error is not caught by your IDE. Worse, it is not caught at runtime because Python will just pass along theDependsobject. This will probably cause an error down the road, but may silently succeed in some cases.I'm willing to implement this if you think it's a good idea.