Python · The language, not just syntax

How do type hints work in Python and what do they buy you?

Mediumtype hintsmypytypingUpdated Oct 2026
Quiz this track

Short answer

Type hints annotate expected types for tooling and readers. CPython stores them but does not enforce them at runtime, so a type checker such as mypy or pyright provides the actual verification.

Python uses gradual typing. You can annotate some functions and leave others dynamic, and the checker infers what it can. Annotations are evaluated as expressions unless postponed, which matters for forward references and import cycles.

python
from typing import Protocol

class Notifier(Protocol):
    def send(self, message: str) -> None: ...

def alert(notifier: Notifier, message: str) -> bool:
    notifier.send(message)
    return True

class EmailNotifier:
    def send(self, message: str) -> None:
        print("email:", message)

alert(EmailNotifier(), "disk full")
Protocol enables structural typing: any object with a compatible send method is accepted, no inheritance required.

The vocabulary

  • Use list[str] and dict[str, int] on modern Python instead of typing.List
  • Use X | None for optional values instead of Optional[X] in new code
  • TypeVar expresses a relationship between input and output types
  • Protocol describes a structural interface without a base class

Hints are not validation. A function annotated to take int happily accepts a string at runtime, and frameworks that claim runtime enforcement generate checks themselves. The value is catching mistakes before deployment and making interfaces self-documenting for editors and reviewers.

from __future__ import annotations postpones evaluation of annotations, which avoids runtime import cycles. Python 3.14 changes annotation evaluation semantics further, so check the target version.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1Are type hints enforced at runtime?
  2. 2What is the difference between a Protocol and an ABC?
  3. 3When would you use TypeVar?
  4. 4How do you handle forward references?

Say it in the interview

Type hints are optional annotations that tools and readers use; CPython itself does not enforce them. I run mypy or pyright in CI so mismatches fail before deployment. I use built-in generics, union syntax and Protocol for structural interfaces. I treat hints as documentation plus static verification, not as input validation, and I validate external data separately.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Python mock interview and get scored feedback on depth, clarity and confidence.