ReviseAlgo Logo

Type Hints & Annotations

Type Hints Basics

Adding optional type annotations to Python — variable annotations, function signatures, return types, and how type hints improve code quality and IDE support.

Interview: Modern Python best practice — interviewers expect type-annotated code in production-quality solutions.

Last Updated: June 12, 2026 8 min read

Type hints (PEP 484, Python 3.5+) add optional static type annotations to Python code. They don't affect runtime behavior but enable static analysis tools (mypy), IDE autocomplete, and self-documenting code. Type hints are the most impactful code quality improvement in modern Python.

Basic Annotations

  • Variables: name: str = "Alice", age: int = 25
  • Function params: def greet(name: str, times: int = 1) -> str:
  • Return type: -> str, -> int, -> None for void functions
  • Class attributes: Annotate in class body or __init__

Key Principles

  • Type hints are optional — unannotated code still works
  • They are not enforced at runtime — Python ignores them during execution
  • Static checkers (mypy, pyright) analyze annotated code without running it
  • Python 3.9+ allows built-in types: list[int] instead of List[int]
  • Python 3.10+ allows X | Y instead of Union[X, Y]

Interview Insight

Write type-annotated code in interviews — it shows professionalism. Be able to explain that type hints are optional, not enforced at runtime, and checked by tools like mypy.

Use Cases

IDE autocomplete — better suggestions and error detection while coding

Static analysis — mypy/pyright catches type errors before runtime

Documentation — type annotations are self-documenting function signatures

Code reviews — reviewers can verify type contracts at a glance

Refactoring — changing types triggers IDE warnings for all affected code

Common Mistakes

Thinking type hints are enforced at runtime — they are only for static analysis

Using old typing syntax (List, Dict) in Python 3.9+ — use list, dict directly

Not annotating return types — return type is the most valuable annotation

Over-annotating obvious types — let inference work for local variables

Forgetting to annotate Optional/None for values that can be missing