PythiloLesson 8 of 20 · progress kept in this browser

Reject unknown keys at a boundary

Pydantic models ignore extra keys by default. That is useful for tolerant readers, but a typo in configuration can then disappear silently. A boundary that owns a fixed vocabulary can reject unknown keys with extra="forbid".

Choose the policy per model. A public response consumer may deliberately tolerate new fields, while a configuration editor should usually report misspellings. Rejecting extras also prevents callers from assuming that arbitrary input fields have been validated merely because the model accepted the rest of the object.

from pydantic import BaseModel, ConfigDict
from pydantic import ValidationError
class Options(BaseModel):
    model_config = ConfigDict(extra="forbid")
    limit: int
try:
    Options(limit=2, limti=3)
except ValidationError as error:
    print(error.errors()[0]["loc"])
Your task

Define Options with extra="forbid", host: str and port: int default 8000. options_data(raw) returns its validated dictionary and rejects misspelled or unknown keys.

The first run loads pydantic into this browser.