optional type

Domain: Validation Typing

🎯 Motivation

Optional marks a value as either a specific type or None. It communicates intent clearly and helps type checkers warn you when you forget to handle the None case.

πŸ“‹ Concrete Example

def find_user(id: int) -> Optional[User]: tells callers the result might be None, and mypy warns if you access .name without checking for None first.

πŸ”— Analogy

Optional is like a parking spot labeled β€œvehicle or empty.” You must check if a car is there before trying to drive it, or you will crash (into a NoneType error).