mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 18:58:04 +00:00
[FastAPI
] Update Annotated
fixes (FAST002
) (#15462)
## Summary The initial purpose was to fix #15043, where code like this: ```python from fastapi import FastAPI, Query app = FastAPI() @app.get("/test") def handler(echo: str = Query("")): return echo ``` was being fixed to the invalid code below: ```python from typing import Annotated from fastapi import FastAPI, Query app = FastAPI() @app.get("/test") def handler(echo: Annotated[str, Query("")]): # changed return echo ``` As @MichaReiser pointed out, the correct fix is: ```python from typing import Annotated from fastapi import FastAPI, Query app = FastAPI() @app.get("/test") def handler(echo: Annotated[str, Query()] = ""): # changed return echo ``` After fixing the issue for `Query`, I realized that other classes like `Path`, `Body`, `Cookie`, `Header`, `File`, and `Form` also looked susceptible to this issue. The last few commits should handle these too, which I think means this will also close #12913. I had to reorder the arguments to the `do_stuff` test case because the new fix removes some default argument values (eg for `Path`: `some_path_param: str = Path()` becomes `some_path_param: Annotated[str, Path()]`). There's also #14484 related to this rule. I'm happy to take a stab at that here or in a follow up PR too. ## Test Plan `cargo test` I also checked the fixed output with `uv run --with fastapi FAST002_0.py`, but it required making a bunch of additional changes to the test file that I wasn't sure we wanted in this PR. --------- Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
parent
48e6541893
commit
1a77a75935
8 changed files with 556 additions and 258 deletions
|
@ -29,13 +29,13 @@ def get_items(
|
|||
|
||||
@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(),
|
||||
some_query_param: str | None = Query(default=None),
|
||||
some_body_param: str = Body("foo"),
|
||||
some_header_param: int = Header(default=5),
|
||||
):
|
||||
# do stuff
|
||||
pass
|
21
crates/ruff_linter/resources/test/fixtures/fastapi/FAST002_1.py
vendored
Normal file
21
crates/ruff_linter/resources/test/fixtures/fastapi/FAST002_1.py
vendored
Normal file
|
@ -0,0 +1,21 @@
|
|||
"""Test that FAST002 doesn't suggest invalid Annotated fixes with default
|
||||
values. See #15043 for more details."""
|
||||
|
||||
from fastapi import FastAPI, Query
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/test")
|
||||
def handler(echo: str = Query("")):
|
||||
return echo
|
||||
|
||||
|
||||
@app.get("/test")
|
||||
def handler2(echo: str = Query(default="")):
|
||||
return echo
|
||||
|
||||
|
||||
@app.get("/test")
|
||||
def handler3(echo: str = Query("123", min_length=3, max_length=50)):
|
||||
return echo
|
Loading…
Add table
Add a link
Reference in a new issue