-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
[feature request] FastAPI rule: prefer Annotated
for Query
, Path
, Body
, Cookie
#12913
Comments
Have a question though: |
Right! I just updated the expected result. 🤓 |
I’d love to take this up! |
I can give some help on that and team up with other people |
Thanks for suggesting this rule. Is there some resource that explains why using |
Here it is https://fastapi.tiangolo.com/tutorial/dependencies/#share-annotated-dependencies |
Thanks, I think this is the important section (we should link it in the rule) https://fastapi.tiangolo.com/tutorial/query-params-str-validations/#advantages-of-annotated |
We should already support all of the mentioned params (Query, File, etc..). Can you please attach your code sample where it doesn't work as expected? I put your example in the Ruff playground (with the needed imports) and it seems to work. Regarding the default value handling - I really liked @tiangolo suggestion of adding |
Indeed! 😱 That's amazing! I assumed it didn't work, but it actually does! 🚀 From: @app.post("/stuff/")
def do_stuff(
some_query_param: str | None = Query(default=None),
some_path_param: str = Path(),
some_body_param: str = Body("foo"),
some_cookie_param: str = Cookie(),
some_header_param: int = Header(default=5),
some_file_param: UploadFile = File(),
some_form_param: str = Form(),
):
# do stuff
pass to: @app.post("/stuff/")
def do_stuff(
some_query_param: Annotated[str | None, Query(default=None)],
some_path_param: Annotated[str, Path()],
some_body_param: Annotated[str, Body("foo")],
some_cookie_param: Annotated[str, Cookie()],
some_header_param: Annotated[int, Header(default=5)],
some_file_param: Annotated[UploadFile, File()],
some_form_param: Annotated[str, Form()],
):
# do stuff
pass The only piece missing would be to move the |
In short
I would like to have a FastAPI rule to prefer
Annotated
for:Body
Cookie
File
Form
Header
Path
Query
and hopefully autofix them.
Depends
andSecurity
are already supported.This would be related to/continuation of #11579
Description
I would like to have
Annotated
as the recommended style/syntax for FastAPI params that support it:Body
Cookie
File
Form
Header
Path
Query
This is very similar to the rule for
Depends
andSecurity
implemented here: #11579The only caveat is that if one of these parameters has a
default
argument (or the first argument), as inQuery(default="foo")
then that would become the new default value. For example:would ideally be transformed into:
or:
The text was updated successfully, but these errors were encountered: