Python Type Hints
CONCEPTS:Python Type Hints
Question Variations
- "Are Python type hints enforced at runtime?"
- "How do `Optional[T]` and `T | None` relate?"
- "When would you use a `Protocol`?"
- "What is the difference between `Any` and `object`?"
Why This Is Asked
Type hints are central to maintainable Python codebases. Interviewers use this topic to assess how you design clear interfaces, use static analysis, and avoid confusing optional, union, generic, and protocol types.
Key Concepts
- Annotations describe intended types but are not generally runtime validation.
- Static checkers such as mypy and pyright analyze annotated code.
- Built-in generics express container element types, such as
list[str]. Protocolcan specify structural interfaces without requiring inheritance.
Question Variations
- “Are Python type hints enforced at runtime?”
- “How do
Optional[T]andT | Nonerelate?” - “When would you use a
Protocol?” - “What is the difference between
Anyandobject?”