Performance: Pydantic model definition
MEDIUM IMPACT
Defines data validation and serialization which impacts API response time and server CPU usage.
from pydantic import BaseModel from typing import List, Dict class Address(BaseModel): street: str city: str class User(BaseModel): id: int name: str email: str address: Address preferences: Dict[str, str] history: List[int]
from pydantic import BaseModel class User(BaseModel): id: int name: str email: str address: dict preferences: dict history: list # Using generic dict and list without strict typing
| Pattern | CPU Usage | Validation Time | Serialization Speed | Verdict |
|---|---|---|---|---|
| Generic dict/list fields | High | Slow | Slower | [X] Bad |
| Typed nested models | Low | Fast | Faster | [OK] Good |