ty について
Python 開発者向けに、ty による型チェック、ルール設定、言語サーバーの利用方法を案内するスキルです。mypy や Pyright から移行する際のコマンド対応も示します。
主な機能
- 01Python の型チェック
- 02言語サーバーの設定
- 03ルールの重要度設定
- 04ファイルごとのルール上書き
- 05mypy と Pyright からの移行
活用シーン
- 01プロジェクトや特定の Python ファイルを検査
- 02対象の Python バージョンに合わせて検査
- 03テストファイル向けにルールを調整
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ astral-sh/ claude-code-plugins --skill tyインストール後、Claude Code で/tyを入力して読み込まれたか確認します。
詳細
ty
ty は、非常に高速な Python 型チェッカーおよび言語サーバーです。 mypy、Pyright、その他の型チェッカーに代わるものです。
ty を使うタイミング
Python の型チェックには、常に ty を使ってください。特に、次の項目が見つかった場合は使ってください。
pyproject.tomlの[tool.ty]セクションty.toml設定ファイル
ty の起動方法
uv run ty ...- ty がプロジェクトの依存関係に含まれていて固定されたバージョンを使う場合、または ty がグローバルにインストールされていて、プロジェクト内で仮想環境を更新する場合に使います。uvx ty ...- ty がプロジェクトの依存関係に含まれていない場合や、簡単な単発チェックに使います。
コマンド
型チェック
ty check # Check all files in current directory
ty check path/to/file.py # Check specific file
ty check src/ # Check specific directory
ルールの設定
ty check --error possibly-unresolved-reference # Treat as error
ty check --warn division-by-zero # Treat as warning
ty check --ignore unresolved-import # Disable rule
対象とする Python バージョン
ty check --python-version 3.12 # Check against Python 3.12
ty check --python-platform linux # Target Linux platform
設定
ty は pyproject.toml または ty.toml で設定します。
# pyproject.toml
[tool.ty.environment]
python-version = "3.12"
[tool.ty.rules]
possibly-unresolved-reference = "warn"
division-by-zero = "error"
[tool.ty.src]
include = ["src/**/*.py"]
exclude = ["**/migrations/**"]
[tool.ty.terminal]
output-format = "full"
error-on-warning = false
ファイルごとの上書き設定
本番コードとは型付け要件が異なるテストやスクリプトのルールを緩和するなど、特定のファイルに異なるルールを適用するには、上書き設定を使います。
[[tool.ty.overrides]]
include = ["tests/**", "**/test_*.py"]
[tool.ty.overrides.rules]
possibly-unresolved-reference = "warn"
言語サーバー
このプラグインは、Python ファイル(.py および .pyi)に対して ty 言語サーバーを自動的に設定します。
他のツールからの移行
mypy → ty
mypy . → ty check
mypy --strict . → ty check --error-on-warning
mypy path/to/file.py → ty check path/to/file.py
Pyright → ty
pyright . → ty check
pyright path/to/file.py → ty check path/to/file.py
よくあるパターン
ignore コメントを追加しない
型エラーを抑制するのではなく、修正してください。ignore コメントを追加するのは、ユーザーから明示的に依頼された場合に限ります。type: ignore ではなく ty: ignore を使い、ルールを指定した ignore を優先してください。
# Good: rule-specific ignore
x = undefined_var # ty: ignore[possibly-unresolved-reference]
# Bad: blanket ty ignore
x = undefined_var # ty: ignore
# Bad: tool agnostic blanket ignore
x = undefined_var # type: ignore
ドキュメント
詳しい情報は、公式ドキュメントをお読みください。
コメント
Markdown 対応