Python
- Use the latest Python language features appropriate for the project's minimum supported version.
Package Management
- Use
uv for dependency management and package execution instead of virtual environments.
- Run scripts with
uv run <script>.
- Add dependencies with
uv add <package>.
Documentation
When users ask about Python standard library modules, use WebFetch to get the latest official documentation from docs.python.org.
Example:
- For
asyncio: https://docs.python.org/3/library/asyncio.html
- For
typing: https://docs.python.org/3/library/typing.html
- For
pathlib: https://docs.python.org/3/library/pathlib.html
Pattern: https://docs.python.org/3/library/<module>.html
Type Hints
- Always use type hints for function signatures, class attributes, and variables where the type is not immediately obvious.
- Avoid using
Any type. Use specific types, TypeVar, or protocols instead.
- Avoid using
# type: ignore comments except in rare cases, mainly in test code.
- Use
from __future__ import annotations for forward references and cleaner type hints.
- Prefer
list[T], dict[K, V], set[T], tuple[T, ...] over typing.List, typing.Dict, etc. (Python 3.9+).
Example:
from __future__ import annotations
def process_items(items: list[str], max_count: int | None = None) -> dict[str, int]:
"""Process items and return a count dictionary."""
result: dict[str, int] = {}
for item in items[:max_count]:
result[item] = result.get(item, 0) + 1
return result
Tests
Write parametrized tests using pytest:
import pytest
@pytest.mark.parametrize(
("input_value", "expected"),
[
("hello", "HELLO"),
("world", "WORLD"),
("", ""),
],
)
def test_uppercase(input_value: str, expected: str) -> None:
assert my_function(input_value) == expected
- Use descriptive parameter names in test cases.
- Type hint test functions with
-> None.
- Group related tests in classes when appropriate.
- Use fixtures for shared setup and teardown.
Code Style
- Follow PEP 8 conventions.
- Use f-strings for string formatting.
1---2name: python3description: Python coding standards, best practices, type hints, and testing patterns. Use when writing or reviewing Python code, implementing tests, or discussing Python language features.4---5# Python
6
7- Use the latest Python language features appropriate for the project's minimum supported version.
8
9## Package Management
10
11- Use `uv` for dependency management and package execution instead of virtual environments.
12- Run scripts with `uv run <script>`.
13- Add dependencies with `uv add <package>`.
14
15## Documentation
16
17When users ask about Python standard library modules, use `WebFetch` to get the latest official documentation from `docs.python.org`.
18
19Example:
20- For `asyncio`: `https://docs.python.org/3/library/asyncio.html`
21- For `typing`: `https://docs.python.org/3/library/typing.html`
22- For `pathlib`: `https://docs.python.org/3/library/pathlib.html`
23
24Pattern: `https://docs.python.org/3/library/<module>.html`
25
26## Type Hints
27
28- **Always** use type hints for function signatures, class attributes, and variables where the type is not immediately obvious.
29- **Avoid** using `Any` type. Use specific types, `TypeVar`, or protocols instead.
30- **Avoid** using `# type: ignore` comments except in rare cases, mainly in test code.
31- Use `from __future__ import annotations` for forward references and cleaner type hints.
32- Prefer `list[T]`, `dict[K, V]`, `set[T]`, `tuple[T, ...]` over `typing.List`, `typing.Dict`, etc. (Python 3.9+).
33
34Example:
35```python
36from __future__ import annotations
37
38def process_items(items: list[str], max_count: int | None = None) -> dict[str, int]:
39 """Process items and return a count dictionary."""
40 result: dict[str, int] = {}
41 for item in items[:max_count]:
42 result[item] = result.get(item, 0) + 1
43 return result
44```
45
46## Tests
47
48Write parametrized tests using `pytest`:
49
50```python
51import pytest
52
53@pytest.mark.parametrize(
54 ("input_value", "expected"),
55 [
56 ("hello", "HELLO"),
57 ("world", "WORLD"),
58 ("", ""),
59 ],
60)
61def test_uppercase(input_value: str, expected: str) -> None:
62 assert my_function(input_value) == expected
63```
64
65- Use descriptive parameter names in test cases.
66- Type hint test functions with `-> None`.
67- Group related tests in classes when appropriate.
68- Use fixtures for shared setup and teardown.
69
70## Code Style
71
72- Follow PEP 8 conventions.
73- Use f-strings for string formatting.