mcpField
P
python-development

python-development

github.com/moizibnyousaf開発1.2k318 回インストール5 週間前に更新
リポジトリ

python-development について

Python プロジェクトの構築と保守を支援する開発スキルです。プロジェクト構成、型ヒント、非同期コード、FastAPI、テスト、ツールの活用方法を扱います。

主な機能

  1. 01モダンなプロジェクト構成
  2. 02型ヒントとジェネリクス
  3. 03非同期 I/O パターン
  4. 04FastAPI の例
  5. 05pytest のテストパターン
  6. 06リンティングと型チェックの指針

活用シーン

  1. 01Python プロジェクトの構成
  2. 02Python コードへの型の追加
  3. 03非同期 I/O の実装
  4. 04FastAPI アプリのテスト

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/moizibnyousaf/ai-agent-skills --skill python-development

インストール後、Claude Code で/python-developmentを入力して読み込まれたか確認します。

詳細

Python 開発

プロジェクトのセットアップ

モダンな Python プロジェクトの構成
my-project/
├── src/
│   └── my_project/
│       ├── __init__.py
│       ├── main.py
│       └── utils.py
├── tests/
│   ├── __init__.py
│   └── test_main.py
├── pyproject.toml
├── README.md
└── .gitignore
pyproject.toml
[project]
name = "my-project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = [
    "fastapi>=0.100.0",
    "pydantic>=2.0",
]

[project.optional-dependencies]
dev = [
    "pytest>=7.0",
    "ruff>=0.1.0",
    "mypy>=1.0",
]

[tool.ruff]
line-length = 88
select = ["E", "F", "I", "N", "W"]

[tool.mypy]
strict = true

型ヒント

from typing import TypeVar, Generic
from collections.abc import Sequence

T = TypeVar('T')

def process_items(items: Sequence[str]) -> list[str]:
    return [item.upper() for item in items]

class Repository(Generic[T]):
    def get(self, id: int) -> T | None: ...
    def save(self, item: T) -> T: ...

非同期パターン

import asyncio
from collections.abc import AsyncIterator

async def fetch_all(urls: list[str]) -> list[dict]:
    async with aiohttp.ClientSession() as session:
        tasks = [fetch_one(session, url) for url in urls]
        return await asyncio.gather(*tasks)

async def stream_data() -> AsyncIterator[bytes]:
    async with aiofiles.open('large_file.txt', 'rb') as f:
        async for chunk in f:
            yield chunk

FastAPI のパターン

from fastapi import FastAPI, Depends, HTTPException
from pydantic import BaseModel

app = FastAPI()

class UserCreate(BaseModel):
    email: str
    name: str

class UserResponse(BaseModel):
    id: int
    email: str
    name: str

@app.post("/users", response_model=UserResponse)
async def create_user(
    user: UserCreate,
    db: Database = Depends(get_db)
) -> UserResponse:
    result = await db.users.create(user.model_dump())
    return UserResponse(**result)

テスト

import pytest
from unittest.mock import AsyncMock, patch

@pytest.fixture
def mock_db():
    db = AsyncMock()
    db.users.get.return_value = {"id": 1, "name": "Test"}
    return db

@pytest.mark.asyncio
async def test_get_user(mock_db):
    result = await get_user(1, db=mock_db)
    assert result["name"] == "Test"
    mock_db.users.get.assert_called_once_with(1)

ベストプラクティス

  • リントとフォーマットには ruff を使用します
  • 厳格モードで mypy を使用します
  • os.path よりも pathlib.Path を優先します
  • データ構造には dataclass または Pydantic を使用します
  • I/O 待ちが発生する処理には asyncio を使用します
  • 非同期リソースには contextlib.asynccontextmanager を使用します

コメント