type hints
Python 3.5
static typing
Python type annotations
type checking

What are type hints in Python 3.5?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Type hints were introduced in Python 3.5 as a standard way to annotate expected types without changing Python's runtime model. They made it possible for editors, linters, and tools such as mypy to reason about code more accurately, while still keeping Python dynamically typed at execution time. In other words, type hints describe intent; they do not make Python behave like Java or C#.

What a Type Hint Looks Like

The core syntax is simple: annotate parameters after the name and the return type after ->.

python
1def greet(name: str) -> str:
2    return f"Hello, {name}"
3
4print(greet("Ada"))

This says that name is expected to be a string and the function returns a string. Python 3.5 will run that function whether or not the caller respects the annotation. The annotation exists for humans and tools.

Variable annotations became more fully standardized later, so in Python 3.5 the most common place to see type hints is in function signatures and typing-module constructs.

Why Type Hints Were Added

Type hints solve several practical problems:

  • they make APIs easier to read
  • they help IDEs offer better autocomplete and refactoring support
  • they allow static checkers to catch mistakes before runtime
  • they make large codebases easier to navigate

For example, compare a function with and without hints:

python
1def total_prices(values):
2    return sum(values)
3
4
5def total_prices_typed(values: list) -> float:
6    return float(sum(values))

The second version communicates more intent immediately. In real Python 3.5 code, you would often use List[float] from typing rather than plain list when you want to describe element types precisely.

Using the typing Module

Python 3.5 introduced the typing module for richer type expressions such as lists, dictionaries, optional values, and unions.

python
1from typing import Dict, List, Optional
2
3
4def find_user(user_id: int, users: Dict[int, str]) -> Optional[str]:
5    return users.get(user_id)
6
7
8def average(scores: List[float]) -> float:
9    return sum(scores) / len(scores)

In Python 3.5, this typing syntax is the normal way to express collection element types. Modern shortcuts such as list[str] did not exist yet.

A few common types from typing are:

  • 'List[T] for lists with element type T'
  • 'Dict[K, V] for dictionaries with key and value types'
  • 'Optional[T] for a value that can also be None'
  • 'Union[A, B] for one of several allowed types'
  • 'Callable[...] for function-like values'

What Type Hints Do Not Do

The biggest misconception is that type hints enforce types at runtime. They do not.

python
1def square(x: int) -> int:
2    return x * x
3
4print(square("abc"))

Python will accept the call and only fail because the operation itself is incompatible with the value, not because the annotation blocked it. To get real checking benefits, you run a static type checker separately.

For example:

bash
mypy app.py

That separate analysis step is where many annotation mistakes are caught.

When Type Hints Help Most

Hints are most valuable on public functions, shared libraries, data-heavy code, and codebases with several contributors. They are less valuable when used mechanically on every tiny local variable without improving understanding.

Good annotations clarify shape and meaning. Weak annotations add noise.

For example, def load_users() -> List[User]: is much more helpful than sprinkling vague hints everywhere while leaving important interfaces untyped.

Common Pitfalls

  • Expecting Python to enforce hints automatically at runtime.
  • Forgetting that Python 3.5 uses typing.List, typing.Dict, and similar names rather than newer built-in generic syntax.
  • Adding annotations without running a checker such as mypy.
  • Over-annotating trivial locals while leaving important API boundaries ambiguous.
  • Treating hints as perfect truth. In Python they are still descriptive metadata, not a hard runtime contract.

Summary

  • Type hints in Python 3.5 are annotations for tools and readers, not runtime enforcement.
  • They were standardized by PEP 484 and commonly use the typing module.
  • Function parameters and return types are the most common annotation targets in Python 3.5 code.
  • Tools such as mypy use hints to catch errors before execution.
  • The main value of type hints is clearer interfaces and earlier feedback in larger codebases.

Course illustration
Course illustration

All Rights Reserved.