提示词库

Python提示词

用于 Python 代码的提示词:添加类型提示、编写 pytest 测试套件,以及分析慢函数的性能。

提示词是英文的,即它们的编写和测试所用的语言。这些页面上的说明和指引已翻译。

  • pytest
  • testing
  • python
100/100 · 139 token

函数的 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 token

为 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().

仅为示意:为展示预期的形式而编写,并非由模型生成。

改成你的

修改需求以符合你的标准,然后检查结果。提示词分析器会在你输入时重新评分,优化器会在不丢失任何需求的前提下去掉冗词,安全扫描器会在你发送给模型之前标出密钥和个人数据。

更多类别