Technical Mid Level

How do you use type hints in Python? What tools do you use for static type checking and how do you handle third-party libraries without type stubs?

Quick Tip

Show a practical adoption strategy: "We added mypy to CI in strict mode for new code and gradually typed existing modules. For untyped libraries, we wrote thin typed wrappers rather than scattering type: ignore everywhere."

What good answers include

Type hints (PEP 484+) add static type annotations without runtime enforcement. Tools: mypy, pyright, or pytype for static checking. Best practices: use typing module types (Optional, Union, List, Dict), type aliases for complex types, Protocol for structural subtyping, and TypeVar for generics. For untyped libraries: use type: ignore comments sparingly, write stub files (.pyi), check typeshed for existing stubs, or use py.typed marker. Strong candidates discuss gradual typing strategy.

What interviewers are looking for

Tests modern Python practices. Candidates who dismiss type hints are missing productivity gains. Those who use them idiomatically with mypy in CI demonstrate professional-grade Python development.

← All Python questions