Performance: Optional query parameters
MEDIUM IMPACT
This affects the server response time and the amount of data processed before sending the response, impacting initial load and interaction speed.
from fastapi import FastAPI from typing import Optional app = FastAPI() @app.get("/items") async def read_items(q: Optional[str] = None): if q: return {"q": q} return {"message": "No query provided"}
from fastapi import FastAPI from typing import Optional app = FastAPI() @app.get("/items") async def read_items(q: str): # q is expected but optional is not declared return {"q": q}
| Pattern | Server Processing | Error Handling | Response Time | Verdict |
|---|---|---|---|---|
| Required query parameter without default | High (validates every request) | High (errors on missing param) | Slower due to error handling | [X] Bad |
| Optional query parameter with default None | Low (skips validation if missing) | None (no errors on missing param) | Faster response for missing param | [OK] Good |