Question presented to candidate:
"Walk me through type hints in Python. If I annotate a parameter as int and pass a string, what happens? Then what are the hints actually for, and who reads them?"
What a strong answer should cover:
- Hints are annotations: expressions attached to parameters, return values and variables; CPython stores them in
__annotations__and never enforces them at runtime - Static type checkers (mypy, pyright) read them without running the code and report mismatches; IDEs use them for completion; this is gradual typing, where unannotated code is treated as
Any - Core vocabulary:
list[int],dict[str, int],X | None(same asOptional[X]),Callable,Any,Literal, plusTypeVarfor generics andProtocolfor structural typing - When annotations are evaluated: eagerly at
deftime up to 3.13, as strings withfrom __future__ import annotations, lazily by default from 3.14 (PEP 649);typing.get_type_hints()resolves forward references - Some libraries do read hints at runtime:
dataclassesbuilds fields from them, Pydantic and FastAPI validate data with them
Code / implementation expected: Optional β annotate a function, pass a wrong type to show nothing happens at runtime, then show what a checker reports or what __annotations__ holds.