jpskill.com
🛠️ 開発・MCP コミュニティ

python-types-contracts

Pythonで公開APIやデータ構造を定義・変更する際に、型定義を明確にし、Pydanticモデルの過剰な使用を避け、変更時の移行を容易にし、あいまいな型指定を減らすことで、より堅牢で保守しやすいコードにするSkill。

📜 元の英語説明(参考)

Use when defining or evolving public interfaces, schema boundaries, or pydantic usage in Python. Also use when annotations are missing on public APIs, pydantic models appear everywhere instead of at trust boundaries, contract changes lack migration guidance, or Any/object types are overused across module boundaries.

🇯🇵 日本人クリエイター向け解説

一言でいうと

Pythonで公開APIやデータ構造を定義・変更する際に、型定義を明確にし、Pydanticモデルの過剰な使用を避け、変更時の移行を容易にし、あいまいな型指定を減らすことで、より堅牢で保守しやすいコードにするSkill。

※ jpskill.com 編集部が日本のビジネス現場向けに補足した解説です。Skill本体の挙動とは独立した参考情報です。

⚡ おすすめ: コマンド1行でインストール(60秒)

下記のコマンドをコピーしてターミナル(Mac/Linux)または PowerShell(Windows)に貼り付けてください。 ダウンロード → 解凍 → 配置まで全自動。

🍎 Mac / 🐧 Linux
mkdir -p ~/.claude/skills && cd ~/.claude/skills && curl -L -o python-types-contracts.zip https://jpskill.com/download/10641.zip && unzip -o python-types-contracts.zip && rm python-types-contracts.zip
🪟 Windows (PowerShell)
$d = "$env:USERPROFILE\.claude\skills"; ni -Force -ItemType Directory $d | Out-Null; iwr https://jpskill.com/download/10641.zip -OutFile "$d\python-types-contracts.zip"; Expand-Archive "$d\python-types-contracts.zip" -DestinationPath $d -Force; ri "$d\python-types-contracts.zip"

完了後、Claude Code を再起動 → 普通に「動画プロンプト作って」のように話しかけるだけで自動発動します。

💾 手動でダウンロードしたい(コマンドが難しい人向け)
  1. 1. 下の青いボタンを押して python-types-contracts.zip をダウンロード
  2. 2. ZIPファイルをダブルクリックで解凍 → python-types-contracts フォルダができる
  3. 3. そのフォルダを C:\Users\あなたの名前\.claude\skills\(Win)または ~/.claude/skills/(Mac)へ移動
  4. 4. Claude Code を再起動

⚠️ ダウンロード・利用は自己責任でお願いします。当サイトは内容・動作・安全性について責任を負いません。

🎯 このSkillでできること

下記の説明文を読むと、このSkillがあなたに何をしてくれるかが分かります。Claudeにこの分野の依頼をすると、自動で発動します。

📦 インストール方法 (3ステップ)

  1. 1. 上の「ダウンロード」ボタンを押して .skill ファイルを取得
  2. 2. ファイル名の拡張子を .skill から .zip に変えて展開(macは自動展開可)
  3. 3. 展開してできたフォルダを、ホームフォルダの .claude/skills/ に置く
    • · macOS / Linux: ~/.claude/skills/
    • · Windows: %USERPROFILE%\.claude\skills\

Claude Code を再起動すれば完了。「このSkillを使って…」と話しかけなくても、関連する依頼で自動的に呼び出されます。

詳しい使い方ガイドを見る →
最終更新
2026-05-18
取得日時
2026-05-18
同梱ファイル
1

📖 Skill本文(日本語訳)

※ 原文(英語/中国語)を Gemini で日本語化したものです。Claude 自身は原文を読みます。誤訳がある場合は原文をご確認ください。

Pythonの型とコントラクト

概要

型ヒントを装飾ではなく、インターフェース設計として扱います。 明示的なコントラクト、安定したパブリックAPI、および境界安全なモデリングに焦点を当てます。

これらは一般的なケースにおける推奨されるデフォルトであり、普遍的なルールではありません。 デフォルトがプロジェクトの制約と矛盾する場合は、より適切な代替案を提案し、トレードオフと補償的な制御について説明します。

いつ使用するか

  • パブリックAPIのシグネチャに型アノテーションがないか、または広すぎる型を使用している場合。
  • Pydanticモデルが信頼境界ではなく、内部ロジック全体に散在している場合。
  • コントラクトの変更が、移行パスなしにダウンストリームのコンシューマーを壊すリスクがある場合。
  • インターフェースが、より狭い型が適用できる場合にAnyobject、または型指定されていない辞書を受け入れる場合。
  • レイヤー(API、DB、ドメイン)間のスキーマ境界が暗黙的であるか、または一貫性がない場合。
  • プロトコル、抽象基底クラス、または構造的部分型を追加または進化させる場合。

使用しない場合:

  • パブリックインターフェースを持たない純粋な実装レベルのコード。
  • 型の厳密さが価値を追加しない、使い捨てのスクリプトまたは一回限りのデータ処理。
  • 安全性よりもタイピングのオーバーヘッドが重要な、パフォーマンスが重要な内部ループ。

クイックリファレンス

  • パブリックAPIを型指定し、コントラクトを明示的に保ちます。
  • 広範なパラメータ型よりも、狭いインターフェースと境界プロトコルを優先します。
  • デフォルトでは、Pydanticを信頼境界で使用し、どこでも使用するわけではありません。
  • コントラクトの変更については、互換性と移行への影響を明示的にします。
  • 深い継承階層よりも、構造的部分型にはProtocolを優先します。
  • パブリック関数からは具体的な型を返し、入力としてプロトコルまたはユニオンを受け入れます。

よくある間違い

  • すべてを同じように型指定すること。 内部ヘルパーは、パブリックAPIと同じ厳密さを必要としません。 プライベートコードに過剰なアノテーションを付けると、安全性なしにノイズが追加されます。
  • Pydanticをどこでも使用すること。 信頼境界(APIイングレス、構成のロード、外部データ)での検証のためにPydanticモデルを予約する代わりに、内部データフローにPydanticモデルを使用すること。
  • 広範な戻り値の型。 パブリック関数からAnyまたはdictを返すと、呼び出し元は構造を推測せざるを得なくなります。 具体的な型またはTypedDictsを返します。
  • コントラクトをサイレントに破棄すること。 バージョニング、非推奨の警告、または移行ノートなしに、関数のシグネチャを変更したり、フィールドを削除したり、受け入れられる型を狭めたりすること。
  • Noneを無視すること。 値が正当に存在しない可能性がある場合に、OptionalまたはNoneとのユニオンを省略すると、実行時までnull安全性のバグが隠されます。

スコープに関する注意

  • これらの推奨事項を、一般的なケースにおける推奨されるデフォルトとして扱い、普遍的なルールとして扱わないでください。
  • デフォルトがプロジェクトの制約と矛盾する場合、または結果を悪化させる場合は、より適切な代替案を提案し、このケースでそれがより優れている理由を説明してください。
  • 逸脱する場合は、トレードオフと補償的な制御(テスト、可観測性、移行、ロールバック)を明示してください。

呼び出しに関する注意

  • このスキルが名前で呼び出されている場合は、ユーザーに通知します:python-design-modularity

参考文献

  • references/typing-policy.md
  • references/contract-evolution.md
  • references/pydantic-boundaries.md
📜 原文 SKILL.md(Claudeが読む英語/中国語)を展開

Python Types and Contracts

Overview

Treat type hints as interface design, not decoration. Focus on explicit contracts, stable public APIs, and boundary-safe modeling.

These are preferred defaults for common cases, not universal rules. When a default conflicts with project constraints, suggest a better-fit alternative and explain tradeoffs and compensating controls.

When to Use

  • Public API signatures lack type annotations or use overly broad types.
  • Pydantic models are scattered throughout internal logic instead of at trust boundaries.
  • Contract changes risk breaking downstream consumers without migration paths.
  • Interfaces accept Any, object, or untyped dicts where narrower types apply.
  • Schema boundaries between layers (API, DB, domain) are implicit or inconsistent.
  • Adding or evolving protocols, abstract base classes, or structural subtyping.

When NOT to use:

  • Pure implementation-level code with no public interface.
  • Throwaway scripts or one-off data munging where type rigor adds no value.
  • Performance-critical inner loops where typing overhead matters more than safety.

Quick Reference

  • Type public APIs and keep contracts explicit.
  • Prefer narrow interfaces and boundary protocols over broad parameter types.
  • Use pydantic at trust boundaries by default, not everywhere.
  • Make compatibility and migration impact explicit for any contract change.
  • Favor Protocol for structural subtyping over deep inheritance hierarchies.
  • Return concrete types from public functions; accept protocols or unions as inputs.

Common Mistakes

  • Typing everything identically. Internal helpers don't need the same rigor as public APIs. Over-annotating private code adds noise without safety.
  • Pydantic everywhere. Using pydantic models for internal data flow instead of reserving them for validation at trust boundaries (API ingress, config loading, external data).
  • Broad return types. Returning Any or dict from public functions forces callers to guess structure. Return concrete types or TypedDicts.
  • Breaking contracts silently. Changing function signatures, removing fields, or narrowing accepted types without versioning, deprecation warnings, or migration notes.
  • Ignoring None. Omitting Optional or union with None when a value can legitimately be absent, hiding null-safety bugs until runtime.

Scope Note

  • Treat these recommendations as preferred defaults for common cases, not universal rules.
  • If a default conflicts with project constraints or worsens the outcome, suggest a better-fit alternative and explain why it is better for this case.
  • When deviating, call out tradeoffs and compensating controls (tests, observability, migration, rollback).

Invocation Notice

  • Inform the user when this skill is being invoked by name: python-design-modularity.

References

  • references/typing-policy.md
  • references/contract-evolution.md
  • references/pydantic-boundaries.md