PythiloLesson 4 of 20 · progress kept in this browser

Reuse constraints with Annotated

Repeating the same bounds on several models makes it easy for their rules to drift. Annotated attaches reusable validation metadata to a type. The resulting alias still communicates its underlying Python type to a type checker, while Pydantic reads the additional constraints at runtime.

Put the alias on collection elements when every element has the rule. A constraint on the collection itself has a different meaning, such as minimum list length. Naming these types also gives an API schema a useful vocabulary.

from typing import Annotated
from pydantic import Field, TypeAdapter
Positive = Annotated[int, Field(gt=0)]
values = TypeAdapter(list[Positive])
print(values.validate_python(["2", 3]))
Your task

Implement positive_ids(raw) with TypeAdapter and an Annotated integer constrained above zero. Validate a list, return list[int], allow an empty list, and reject any nonpositive or invalid element.

The first run loads pydantic into this browser.