Type hints in modern Python

Posted 2026-02-04 on the pythondeck.com blog

Type hints have gone from "optional curiosity" to "expected on serious codebases" in five years. Here's what a working subset looks like in 2026.

The basics

def total(items: list[float], tax: float = 0.0) -> float:
    return sum(items) * (1 + tax)

Optional and unions

Use the new pipe syntax instead of Optional / Union:

def find(name: str) -> User | None: ...

Generics

def first[T](items: list[T]) -> T:
    return items[0]

(Python 3.12+ class/function type parameter syntax; before that, use TypeVar.)

Protocols (duck typing, statically checked)

from typing import Protocol

class Closeable(Protocol):
    def close(self) -> None: ...

def shut(x: Closeable) -> None:
    x.close()

TypedDict and dataclasses

For JSON-shaped data, use TypedDict; for Python records, use @dataclass. Both give the type checker something concrete to verify.

Tools

Run mypy or pyright in CI. Don't aim for 100% strictness on day one - start with --ignore-missing-imports and tighten over time.

« all blog posts Browse tutorials