-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[
fastapi
] Implement FAST001
(fastapi-redundant-response-model
) …
…and `FAST002` (`fastapi-non-annotated-dependency`) (#11579) ## Summary Implements ruff specific role for fastapi routes, and its autofix. ## Test Plan `cargo test` / `cargo insta review`
- Loading branch information
Showing
14 changed files
with
1,009 additions
and
5 deletions.
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
crates/ruff_linter/resources/test/fixtures/fastapi/FAST001.py
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,110 @@ | ||
from typing import List, Dict | ||
|
||
from fastapi import FastAPI, APIRouter | ||
from pydantic import BaseModel | ||
|
||
app = FastAPI() | ||
router = APIRouter() | ||
|
||
|
||
class Item(BaseModel): | ||
name: str | ||
|
||
|
||
# Errors | ||
|
||
|
||
@app.post("/items/", response_model=Item) | ||
async def create_item(item: Item) -> Item: | ||
return item | ||
|
||
|
||
@app.post("/items/", response_model=list[Item]) | ||
async def create_item(item: Item) -> list[Item]: | ||
return item | ||
|
||
|
||
@app.post("/items/", response_model=List[Item]) | ||
async def create_item(item: Item) -> List[Item]: | ||
return item | ||
|
||
|
||
@app.post("/items/", response_model=Dict[str, Item]) | ||
async def create_item(item: Item) -> Dict[str, Item]: | ||
return item | ||
|
||
|
||
@app.post("/items/", response_model=str) | ||
async def create_item(item: Item) -> str: | ||
return item | ||
|
||
|
||
@app.get("/items/", response_model=Item) | ||
async def create_item(item: Item) -> Item: | ||
return item | ||
|
||
|
||
@app.get("/items/", response_model=Item) | ||
@app.post("/items/", response_model=Item) | ||
async def create_item(item: Item) -> Item: | ||
return item | ||
|
||
|
||
@router.get("/items/", response_model=Item) | ||
async def create_item(item: Item) -> Item: | ||
return item | ||
|
||
|
||
# OK | ||
|
||
|
||
async def create_item(item: Item) -> Item: | ||
return item | ||
|
||
|
||
@app("/items/", response_model=Item) | ||
async def create_item(item: Item) -> Item: | ||
return item | ||
|
||
|
||
@cache | ||
async def create_item(item: Item) -> Item: | ||
return item | ||
|
||
|
||
@app.post("/items/", response_model=str) | ||
async def create_item(item: Item) -> Item: | ||
return item | ||
|
||
|
||
@app.post("/items/") | ||
async def create_item(item: Item) -> Item: | ||
return item | ||
|
||
|
||
@app.post("/items/", response_model=str) | ||
async def create_item(item: Item): | ||
return item | ||
|
||
|
||
@app.post("/items/", response_model=list[str]) | ||
async def create_item(item: Item) -> Dict[str, Item]: | ||
return item | ||
|
||
|
||
@app.post("/items/", response_model=list[str]) | ||
async def create_item(item: Item) -> list[str, str]: | ||
return item | ||
|
||
|
||
@app.post("/items/", response_model=Dict[str, int]) | ||
async def create_item(item: Item) -> Dict[str, str]: | ||
return item | ||
|
||
|
||
app = None | ||
|
||
|
||
@app.post("/items/", response_model=Item) | ||
async def create_item(item: Item) -> Item: | ||
return item |
68 changes: 68 additions & 0 deletions
68
crates/ruff_linter/resources/test/fixtures/fastapi/FAST002.py
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,68 @@ | ||
from fastapi import ( | ||
FastAPI, | ||
APIRouter, | ||
Query, | ||
Path, | ||
Body, | ||
Cookie, | ||
Header, | ||
File, | ||
Form, | ||
Depends, | ||
Security, | ||
) | ||
from pydantic import BaseModel | ||
|
||
app = FastAPI() | ||
router = APIRouter() | ||
|
||
|
||
# Errors | ||
|
||
@app.get("/items/") | ||
def get_items( | ||
current_user: User = Depends(get_current_user), | ||
some_security_param: str = Security(get_oauth2_user), | ||
): | ||
pass | ||
|
||
|
||
@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 | ||
|
||
|
||
# Unchanged | ||
|
||
|
||
@app.post("/stuff/") | ||
def do_stuff( | ||
no_default: Body("foo"), | ||
no_type_annotation=str, | ||
no_fastapi_default: str = BaseModel(), | ||
): | ||
pass | ||
|
||
|
||
# OK | ||
|
||
@app.post("/stuff/") | ||
def do_stuff( | ||
some_path_param: Annotated[str, Path()], | ||
some_cookie_param: Annotated[str, Cookie()], | ||
some_file_param: Annotated[UploadFile, File()], | ||
some_form_param: Annotated[str, Form()], | ||
some_query_param: Annotated[str | None, Query()] = None, | ||
some_body_param: Annotated[str, Body()] = "foo", | ||
some_header_param: Annotated[int, Header()] = 5, | ||
): | ||
pass |
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
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
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
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,27 @@ | ||
//! FastAPI-specific rules. | ||
pub(crate) mod rules; | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::convert::AsRef; | ||
use std::path::Path; | ||
|
||
use anyhow::Result; | ||
use test_case::test_case; | ||
|
||
use crate::registry::Rule; | ||
use crate::test::test_path; | ||
use crate::{assert_messages, settings}; | ||
|
||
#[test_case(Rule::FastApiRedundantResponseModel, Path::new("FAST001.py"))] | ||
#[test_case(Rule::FastApiNonAnnotatedDependency, Path::new("FAST002.py"))] | ||
fn rules(rule_code: Rule, path: &Path) -> Result<()> { | ||
let snapshot = format!("{}_{}", rule_code.as_ref(), path.to_string_lossy()); | ||
let diagnostics = test_path( | ||
Path::new("fastapi").join(path).as_path(), | ||
&settings::LinterSettings::for_rule(rule_code), | ||
)?; | ||
assert_messages!(snapshot, diagnostics); | ||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.