ruff/crates/ruff_linter/resources/test/fixtures/fastapi/FAST001.py
TomerBin 053243635c
[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`
2024-07-21 18:28:10 +00:00

110 lines
2 KiB
Python

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