Python Type Hints: Documentation Your Linter Can Read

Type hints describe expected values, but Python itself does not enforce them while running. Static checkers and IDEs use the annotations before execution; libraries such as FastAPI can also inspect them for validation and API documentation.
Why it exists
Python is dynamically typed, which is flexible but can lead to bugs that are only found at runtime. Type hints were introduced to allow for static analysis, catching type-related errors before the code is ever executed, improving code quality and maintainability, especially in large projects.
The mental model
Think of type hints as optional, machine-readable documentation. You're adding labels to your code's "pipes"—function arguments, variables, and return values—that specify the kind of data that should flow through them. The Python interpreter itself doesn't look at these labels when running the code. Instead, separate "inspector" tools, called static type checkers, read them to find potential mismatches and bugs ahead of time.
How it works
Type hints use standard Python syntax. For a variable, you write variable: type. For a function, you annotate arguments and the return value: def my_func(param: str) -> int:. For more complex types like lists or dictionaries, you import them from the typing module, for example from typing import List, Dict. A list of strings is List[str]. A special type, Any, can be used to indicate that a value can be of any type, allowing for gradual adoption of type hints in an existing codebase.
When to use it
Use type hints in any project that will be maintained over time or worked on by a team. They are invaluable for improving code clarity, enabling powerful IDE features like code completion and refactoring, and catching a whole class of bugs before they make it to production. They are a standard feature in modern Python frameworks like FastAPI.
When not to use it
For very small, short-lived scripts, the overhead of adding types might not be worth the benefit. Also, since type hints are for static analysis, they don't replace runtime validation of external data, like user input or API responses. You still need to validate that data at the boundaries of your system.
One canonical example
A simple function to greet a user, with type hints for the argument and return value:
def greeting(name: str) -> str:return 'Hello ' + name
A static type checker like mypy would flag an error if you tried to call this function with a number, like greeting(123), even though Python itself would only fail at runtime when trying to concatenate a string and an integer.
Interview question
What is the primary mechanism by which Python type hints improve code reliability?
- a.They enforce strict type checking during the program's runtime execution.
- b.They automatically convert incorrect data types to the expected types at runtime.
- c.They enable static analysis tools to identify potential type mismatches before execution.Correct
- d.They provide a mechanism for validating all external input data at the system's boundaries.
Why? this is the answer
Type hints are ignored by the Python interpreter at runtime. Their primary role is to allow static analysis tools to check for type consistency and potential errors before the code is ever executed, improving reliability. They do not perform runtime validation of external data.
Just read this? Test yourself on what you have been reading.
Read the original → peps.python.org
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on python — each one lists the topics its interview covers.
See open roles