python-type-safety は何をしますか?
Pythonの型安全性:型ヒント、ジェネリクス、プロトコル、厳格な型チェック。型アノテーションの追加、ジェネリッククラスの実装、構造的インターフェースの定義、mypy/pyrightの設定に使用します。
概要
このスキルは、Pythonプロジェクトで型安全性を強化するためのものです。型ヒント(int, str, Optional, Unionなど)、ジェネリクス(TypeVar, Generic)、プロトコル(Protocol)を使用した構造的サブタイピング、そしてmypyやpyrightによる厳格な型チェック設定をサポートします。
使用例
型アノテーションの追加
def greet(name: str) -> str:
return f"Hello, {name}"
ジェネリッククラスの実装
from typing import TypeVar, Generic
T = TypeVar('T')
class Stack(Generic[T]):
def __init__(self) -> None:
self._items: list[T] = []
def push(self, item: T) -> None:
self._items.append(item)
def pop(self) -> T:
return self._items.pop()
プロトコルの定義
from typing import Protocol
class Drawable(Protocol):
def draw(self) -> None: ...
def render(obj: Drawable) -> None:
obj.draw()
mypy/pyrightの設定
mypy.ini:
[mypy]
strict = True
check_untyped_defs = True
disallow_any_unimported = True
ignore_missing_imports = False
pyproject.toml:
[tool.pyright]
strict = [
"src/**/*.py",
]
exclude = ["tests/**"]
対応プラットフォーム
- Claude Code
- Cursor
- Codex
関連情報
- 公式ドキュメント: Python typing
- mypy: mypy-lang.org
- pyright: github.com/microsoft/pyright