new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Pydantic · Concepts · all subjects

fields & aliases

3 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Field metadata types: field-specific vs type-specific

Field metadata falls into two categories: field-specific metadata such as deprecated and alias that only has meaning when attached to a field, and type-specific metadata including constraints like gt and max_length, plus metadata affecting JSON Schema like description and title.

Assignment form vs Annotated pattern for Field

The Field() function can be attached using the assignment form (class User(BaseModel): first_name: str = Field(alias='name')) or the Annotated pattern (class Model(BaseModel): value: Annotated[int, Field(deprecated=True)] = 1). You should use the assignment form for metadata that has meaning for static type checkers, including alias, default and default_factory. The Annotated pattern has advantages for avoiding confusion about default values and providing multiple metadata elements.

Field-specific metadata must apply to whole union type

Field-specific metadata can only be used on the top-level type. A common pitfall is placing field-specific metadata inside a union rather than on the outer type. For example, use field_ok: Annotated[int | None, Field(deprecated=True)] = None instead of field_bad: Annotated[int, Field(deprecated=True)] | None = None, so the deprecated metadata applies to the whole union.

Give your agent this brain