guides

Type Hints and Static Typing

How type annotations improve compilation quality and performance.

Published May 30, 2026

Why Type Hints Help

Type annotations give the compiler more information to generate efficient machine code. Typed variables can avoid generic Python object boxing.

Basic Type Hints

def process(data: list[int]) -> int:
    total: int = 0
    for x in data:
        total += x
    return total

Supported Type Constructs

  • int, float, bool, str
  • list[T], dict[K, V], tuple[T, ...]
  • Optional[T], Union[T, U]

Type Stability

The compiler generates specialised code for the first observed type. If a variable later changes type, it may deoptimise:

# Good: x stays int
x: int = 0
for i in range(1000):
    x += i

# Slower: type changes
x = 0
for i in range(1000):
    if i == 500:
        x = 1.5  # deoptimisation point

Limitations

  • Generic classes with custom type parameters may be treated as object.
  • Recursive type aliases are not fully resolved.