Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Support PEP 593 Annotated for specifying dependencies and parameters #3323

@nzig

Description

@nzig

First check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.
  • After submitting this, I commit to:
    • Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.
    • Or, I already hit the "watch" button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.
    • Implement a Pull Request for a confirmed bug.

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions