Type Stability Best Practices

May 30, 2026 | 5 min read

The Rule

Once a variable has a type, keep it that type for the entire function.

Good Example

def sum_ints(data):
    total: int = 0
    for x in data:
        total += x
    return total

Bad Example

def mixed(data):
    total = 0
    for x in data:
        if x > 10:
            total = str(total)  # type change!
        total += x