Prompt library

Python prompts

Prompts for Python code: adding type hints, writing pytest suites, and profiling slow functions.

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

pytest suite for a function

Writes pytest tests covering normal cases, boundaries, and errors for a function you provide.

Use it when: A function has no tests and you want coverage of edge cases before you refactor it.

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>

Variables

{{function}}
The function source, with its imports.

Example input

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

Example output

```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.

Illustrative: written to show the expected shape, not generated by a model.

  • type hints
  • mypy
  • python
100/100 · 150 tokens

Add type hints to Python code

Adds precise type hints that pass a strict type checker, without changing behavior.

Use it when: Turning on mypy or pyright for an untyped module.

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>

Variables

{{module}}
The Python source to annotate.

Example input

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

Example output

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

Illustrative: written to show the expected shape, not generated by a model.

Make it yours

Edit the requirements to match your standards, then check the result. Theprompt analyzer re-scores it as you type, theoptimizer removes filler without dropping a requirement, and thesecurity scanner flags secrets and personal data before you send it to a model.

More categories