Checking your account

You can read the lesson while its saved answers and constructions load. Learning controls will open once that work is ready.

Pydantic Models and Optional Fields

Does an Empty Note Mean the Field Was Omitted?

Pydantic is a Python library that checks incoming data against declared types and other rules. We describe a record in a model class, then ask the library to construct a validated instance or explain why the input does not satisfy it. A Pydantic model here is a data contract, not a machine learning model.

Suppose a report request must include a title and a note field. The note may be None, meaning the requester explicitly has no note. The number of copies may be omitted, in which case we use a declared default. These choices look similar in an informal description, but a program needs to distinguish them.

These pieces demonstrate selected field rules. They do not run Pydantic. JSON null represents Python None; the Python challenges check the full model.

Step 1 of 1

Two Questions for Every Field

Ask whether the caller must supply the key, then ask which values that key allows. These are Pydantic v2 declarations.

  1. title: str

    May omit the key?
    No
    May supply None?
    No
  2. note: str | None

    May omit the key?
    No
    May supply None?
    Yes
  3. comment: str | None = None

    May omit the key?
    Yes; its default is None
    May supply None?
    Yes
  4. status: str = 'draft'

    May omit the key?
    Yes; its default is 'draft'
    May supply None?
    No
Optional[str] and str | None allow the same value alternatives. Neither spelling supplies a default by itself.

Describe One Record with a Model Class

Allowing null does not supply a missing key. The copies default applies only when copies is absent; it does not replace an explicit zero.