Advanced·7 min·advanced · typing
Type Hints
Type Hints
Hints describe what types your functions expect and return. Python doesn't enforce them at runtime — tools like mypy, pyright, and your editor do.
Cheat sheet
x: int,name: str,flag: boollist[int],dict[str, int],tuple[int, str](Python 3.9+)Optional[X]/X | None— nullableCallable[[int, int], int]— function that takes two ints, returns intIterable[X]— anything you canfor ... in
Why bother
- Editor autocomplete catches typos and wrong-type calls
- Self-documenting — type hints often replace half your docstring
- Forces you to think about your interfaces
Try it
- Add type hints to a function you've written.
- What's the difference between
listandlist[int]?