Pydantic — The Bouncer at the Door
Analogy: A bouncer checking IDs. Wrong ID? You don't get in.
from pydantic import BaseModel, Field
class MovieFeatures(BaseModel):
budget: float = Field(..., gt=0, description="Budget in millions")
runtime: float = Field(..., gt=0, le=300, description="Runtime in minutes")
genre_action: int = Field(..., ge=0, le=1)
genre_comedy: int = Field(..., ge=0, le=1)
Send {"budget": -5, "runtime": 120, ...} and you get back:
{
"detail": [{"loc": ["body", "budget"],
"msg": "ensure this value is greater than 0"}]
}
Automatic 422 error. No manual if-checks needed. The bouncer does the work.