- 最終更新
- 2026/08/17
3秒でわかる
Pythonの引数や戻り値に想定する型を書き添える記法。実行は止めない代わりに、検査ツールと補完が誤りを先に見つけます。
30秒図解
もう少し詳しく
どういうものか
型ヒントは、変数や引数、
なぜ必要か
引数の data にNone にNone を
具体例
from dataclasses import dataclass
@dataclass
class User:
id: int
name: str
def find_user(users: list[User], user_id: int) -> User | None:
for u in users:
if u.id == user_id:
return u
return None
def total_price(prices: dict[str, int], tax: float = 0.1) -> int:
return round(sum(prices.values()) * (1 + tax))検査は
pip install mypy
mypy app.pyつまずきやすいところ
書いたのだからmypy をOptional をNone を| None を[] や {} を
似た用語との違い
| 語 | 内容 |
|---|---|
| 型ヒント | 想定する |
| 静的型付け | 言語側が |
| 実行時検証 | pydantic などが |
覚え方
型ヒントは


