Type hints zachytí chyby před spuštěním, zlepší IDE autocomplete a slouží jako dokumentace.
Základní typy¶
from typing import Optional def greet(name: str, times: int = 1) -> str: return f”Hello {name}! ” * times def find_user(user_id: int) -> Optional[dict]: … # Může vrátit None
Python 3.10+¶
def process(data: str | None) -> list[int]: …
Generics a Protocol¶
from typing import TypeVar, Protocol T = TypeVar(‘T’) def first(items: list[T]) -> T: return items[0] class Renderable(Protocol): def render(self) -> str: … def display(item: Renderable) -> None: print(item.render())
mypy¶
pyproject.toml¶
[tool.mypy] strict = true warn_return_any = true
Spuštění¶
mypy src/
Klíčový takeaway¶
Type hints všude + mypy –strict. Zachytí chyby před runtime, zlepší DX.