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
- Define the public contract before implementation details.
- Validate at the system boundary and keep domain logic independently testable.
- Make failure behavior explicit with stable status codes and safe error bodies.
- 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.
Core concept check
Quiz · 10 pts
Which statement best reflects Typed Python for APIs?
Select an answerProduction judgment
Quiz · 10 pts
Which practice produces the most maintainable API?
Select an answerImplement 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.