Module 1Lesson 1 of 2418 min

Typed Python for APIs

Use type hints as executable API contracts and keep transformation logic pure.

Production principles

  • Type hints make request and response intent explicit
  • Pure functions are easier to test than route-bound logic
  • Optional values must be modeled deliberately

Working example

python
from typing import Any
def normalize_tag(value: str) -> str:    return value.strip().lower().replace(" ", "-")

Engineering checklist

  1. Define the public contract before implementation details.
  2. Validate at the system boundary and keep domain logic independently testable.
  3. Make failure behavior explicit with stable status codes and safe error bodies.
  4. Test the happy path, invalid input, missing resources, and authorization boundaries.

FastAPI turns Python type information into runtime validation and OpenAPI documentation, but types do not replace careful domain design.

Challenges

Pass every challenge to complete this lesson.

3 remaining
  1. Core concept check

    Quiz · 10 pts

    Which statement best reflects Typed Python for APIs?

    Select an answer
  2. Production judgment

    Quiz · 10 pts

    Which practice produces the most maintainable API?

    Select an answer
  3. Implement normalize_tag

    Code challenge · 20 pts

    Complete the Python function normalize_tag. Keep the signature unchanged and return JSON-compatible values. Hidden tests include edge cases.

    Your function must be named normalize_tag and return a value.

Sign in to save your progress.