Performance: Boolean query parameters
MEDIUM IMPACT
This affects how quickly the server parses and validates query parameters, impacting response time and user experience.
from fastapi import FastAPI app = FastAPI() @app.get("/items") async def read_items(active: bool = False): return {"active": active}
from fastapi import FastAPI, Query app = FastAPI() @app.get("/items") async def read_items(active: str = Query("false")): is_active = active.lower() == "true" return {"active": is_active}
| Pattern | CPU Parsing Cost | Validation Overhead | Error Risk | Verdict |
|---|---|---|---|---|
| Manual string parsing | High (string operations per request) | Manual, error-prone | Higher (case sensitivity, typos) | [X] Bad |
| Native boolean parameter | Low (automatic parsing) | Automatic, robust | Low (built-in validation) | [OK] Good |