Skip to content

Type Annotations in Python

Type annotations allow you to indicate the expected data types of variables, function arguments, and return values. They help with code readability and enable better tooling and static analysis.

Basic Usage

1
2
3
4
5
def add(x: int, y: int) -> int:
    return x + y

name: str = "Alice"
age: int = 30

Common Types

  • int, float, str, bool
  • List, Dict, Tuple, Set (from typing module)

Example with Collections

1
2
3
4
from typing import List, Dict

def process_items(items: List[str]) -> Dict[str, int]:
    return {item: len(item) for item in items}

Optional and Union

1
2
3
4
5
6
7
from typing import Optional, Union

def get_name(user: dict) -> Optional[str]:
    return user.get("name")

def parse_value(val: Union[int, str]) -> str:
    return str(val)

Benefits

  • Better editor support
  • Early error detection
  • Improved documentation

Resources