プロンプトライブラリ

Pythonのプロンプト

Python コード向けのプロンプト:型ヒントの追加、pytest スイートの作成、遅い関数のプロファイリング。

プロンプト本体は、作成・テストした言語である英語です。これらのページの説明と案内文は翻訳されています。

  • pytest
  • testing
  • python
100/100 · 139 トークン

関数の pytest スイート

渡した関数について、通常のケース、境界値、エラーをカバーする pytest テストを書きます。

使う場面: 関数にテストがなく、リファクタリングの前にエッジケースをカバーしたいとき。

prompt
You are a senior Python engineer writing tests with pytest.

## Task
Write a pytest test module for the function below.

## Requirements
- Cover one normal case, every boundary value, and every exception the function raises.
- Use pytest.mark.parametrize where 3 or more cases share one assertion.
- Name each test test_<function>_<condition>_<expected result>.
- Use only pytest and the standard library.
- Do not test behavior the function does not implement.

## Output format
Return one Python code block containing the complete test module, followed by a bulleted list of any behavior in the function that looks like a bug.

## Input
<function>
{{function}}
</function>

変数

{{function}}
関数のソースとそのインポート。

入力例

function: def parse_duration(text: str) -> int, which converts "1h30m" to seconds and raises ValueError on bad input.

出力例

```python
@pytest.mark.parametrize("text, expected", [("1h", 3600), ("30m", 1800), ("1h30m", 5400)])
def test_parse_duration_valid_returns_seconds(text, expected):
    assert parse_duration(text) == expected
```
- `parse_duration("")` returns 0 instead of raising ValueError.

例示です。期待される形を示すために書いたもので、モデルが生成したものではありません。

  • type hints
  • mypy
  • python
100/100 · 150 トークン

Python コードへの型ヒント追加

動作を変えずに、厳密な型チェッカーを通る正確な型ヒントを追加します。

使う場面: 型のないモジュールで mypy や pyright を有効にするとき。

prompt
You are a senior Python engineer adding type hints for a strict type checker.

## Task
Add type hints to the Python module below so it passes mypy in strict mode.

## Requirements
- Do not change runtime behavior, names, or signatures beyond adding annotations.
- Never use Any; use object, a Protocol, or a TypeVar where the type varies.
- Use built-in generics such as list[str] and the X | None syntax.
- Use a TypedDict or dataclass for every dictionary with fixed keys.

## Output format
Return one Python code block containing the annotated module, followed by a bulleted list of every annotation you were unsure of and what you assumed.

## Input
<module>
{{module}}
</module>

変数

{{module}}
型を付ける Python のソース。

入力例

module: a functions file with def load_users(path) that returns a list of dicts with name and email.

出力例

```python
class User(TypedDict):
    name: str
    email: str

def load_users(path: str | Path) -> list[User]: ...
```
- `path`: assumed to accept both str and Path, as it is passed to open().

例示です。期待される形を示すために書いたもので、モデルが生成したものではありません。

自分用に調整する

要件を自分の基準に合わせて編集し、結果をチェックしましょう。プロンプトアナライザーは入力に合わせて再採点し、オプティマイザーは要件を落とさずに冗長な表現を削り、セキュリティスキャナーはモデルに送る前に秘密情報や個人データを検出します。

その他のカテゴリ