Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/fastapi/FAST002_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
"""Test FAST002 ellipsis handling."""

from fastapi import Body, Cookie, FastAPI, Header, Query

app = FastAPI()


# Cases that should be fixed - ellipsis should be removed


@app.get("/test1")
async def test_ellipsis_query(
# This should become: param: Annotated[str, Query(description="Test param")]
param: str = Query(..., description="Test param"),
) -> str:
return param


@app.get("/test2")
async def test_ellipsis_header(
# This should become: auth: Annotated[str, Header(description="Auth header")]
auth: str = Header(..., description="Auth header"),
) -> str:
return auth


@app.post("/test3")
async def test_ellipsis_body(
# This should become: data: Annotated[dict, Body(description="Request body")]
data: dict = Body(..., description="Request body"),
) -> dict:
return data


@app.get("/test4")
async def test_ellipsis_cookie(
# This should become: session: Annotated[str, Cookie(description="Session ID")]
session: str = Cookie(..., description="Session ID"),
) -> str:
return session


@app.get("/test5")
async def test_simple_ellipsis(
# This should become: id: Annotated[str, Query()]
id: str = Query(...),
) -> str:
return id


@app.get("/test6")
async def test_multiple_kwargs_with_ellipsis(
# This should become: param: Annotated[str, Query(description="Test", min_length=1, max_length=10)]
param: str = Query(..., description="Test", min_length=1, max_length=10),
) -> str:
return param


# Cases with actual default values - these should preserve the default


@app.get("/test7")
async def test_with_default_value(
# This should become: param: Annotated[str, Query(description="Test")] = "default"
param: str = Query("default", description="Test"),
) -> str:
return param


@app.get("/test8")
async def test_with_default_none(
# This should become: param: Annotated[str | None, Query(description="Test")] = None
param: str | None = Query(None, description="Test"),
) -> str:
return param or "empty"


@app.get("/test9")
async def test_mixed_parameters(
# First param should be fixed with default preserved
optional_param: str = Query("default", description="Optional"),
# Second param should not be fixed because of the preceding default
required_param: str = Query(..., description="Required"),
# Third param should be fixed with default preserved
another_optional_param: int = Query(42, description="Another optional"),
) -> str:
return f"{required_param}-{optional_param}-{another_optional_param}"
2 changes: 2 additions & 0 deletions crates/ruff_linter/src/rules/fastapi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ mod tests {
#[test_case(Rule::FastApiRedundantResponseModel, Path::new("FAST001.py"))]
#[test_case(Rule::FastApiNonAnnotatedDependency, Path::new("FAST002_0.py"))]
#[test_case(Rule::FastApiNonAnnotatedDependency, Path::new("FAST002_1.py"))]
#[test_case(Rule::FastApiNonAnnotatedDependency, Path::new("FAST002_2.py"))]
#[test_case(Rule::FastApiUnusedPathParameter, Path::new("FAST003.py"))]
fn rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.name(), path.to_string_lossy());
Expand Down Expand Up @@ -56,6 +57,7 @@ mod tests {
// since `typing.Annotated` was added in Python 3.9
#[test_case(Rule::FastApiNonAnnotatedDependency, Path::new("FAST002_0.py"))]
#[test_case(Rule::FastApiNonAnnotatedDependency, Path::new("FAST002_1.py"))]
#[test_case(Rule::FastApiNonAnnotatedDependency, Path::new("FAST002_2.py"))]
fn rules_py38(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}_py38", rule_code.name(), path.to_string_lossy());
let diagnostics = test_path(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,19 +275,42 @@ fn create_diagnostic(
.collect::<Vec<_>>()
.join(", ");

seen_default = true;
format!(
"{parameter_name}: {binding}[{annotation}, {default_}({kwarg_list})] \
= {default_value}",
// Check if the default argument is ellipsis (...), which in FastAPI means "required"
let is_default_argument_ellipsis = matches!(
dependency_call.default_argument.value(),
ast::Expr::EllipsisLiteral(_)
);

if is_default_argument_ellipsis && seen_default {
// For ellipsis after a parameter with default, can't remove the default
return Ok(None);
}

if !is_default_argument_ellipsis {
// For actual default values, mark that we've seen a default
seen_default = true;
}

let base_format = format!(
"{parameter_name}: {binding}[{annotation}, {default_}({kwarg_list})]",
parameter_name = parameter.name,
annotation = checker.locator().slice(parameter.annotation.range()),
default_ = checker
.locator()
.slice(map_callable(parameter.default).range()),
default_value = checker
);

if is_default_argument_ellipsis {
// For ellipsis, don't add a default value since the parameter
// should remain required after conversion to Annotated
base_format
} else {
// For actual default values, preserve them
let default_value = checker
.locator()
.slice(dependency_call.default_argument.value().range()),
)
.slice(dependency_call.default_argument.value().range());
format!("{base_format} = {default_value}")
}
}
_ => {
if seen_default {
Expand Down
Loading