Skip to main content

Checkpoint: Typing

advanced30 min readLesson 120 of 169

Design a generic Repository protocol and its in-memory implementation.

Checkpoint — a typed repository

Build:

  1. class Repository(Protocol[T]) with add(item: T) -> None, get(key: str) -> Optional[T], all() -> List[T] (protocol bodies use ...).
  2. class MemoryRepository(Generic[T]) implementing it, constructed with a key_fn used to derive each item's key.
  3. seed(repo, users) adding every user.

Contract detail: add keeps the FIRST item for a key (later duplicates are ignored — get() and all() still return the first).