python
Python coding standards, best practices, type hints, and testing patterns. Use when writing or reviewing Python code, implementing tests, or discussing Python language features.
When & Why to Use This Skill
This Claude skill optimizes Python development by enforcing modern coding standards, comprehensive type hinting, and robust testing patterns. It ensures codebases remain maintainable, scalable, and aligned with PEP 8 and the latest Python features, while also streamlining dependency management with uv and automating documentation retrieval.
Use Cases
- Code Quality Assurance: Reviewing Python code to identify missing type hints, PEP 8 violations, or non-idiomatic patterns to ensure high-quality production code.
- Modernizing Dependencies: Transitioning projects from traditional virtual environments to 'uv' for faster, more reliable package management and script execution.
- Automated Test Generation: Creating parametrized pytest suites with descriptive names and proper type hints to ensure high code coverage and functional correctness.
- Documentation Integration: Automatically fetching and applying the latest official Python documentation for standard library modules like asyncio, typing, and pathlib.
- Refactoring Legacy Code: Updating older Python scripts to use modern features like f-strings, 'from __future__ import annotations', and native collection type hints (Python 3.9+).
| name | Python |
|---|---|
| description | Python coding standards, best practices, type hints, and testing patterns. Use when writing or reviewing Python code, implementing tests, or discussing Python language features. |
Python
- Use the latest Python language features appropriate for the project's minimum supported version.
Package Management
- Use
uvfor 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
Anytype. Use specific types,TypeVar, or protocols instead. - Avoid using
# type: ignorecomments except in rare cases, mainly in test code. - Use
from __future__ import annotationsfor forward references and cleaner type hints. - Prefer
list[T],dict[K, V],set[T],tuple[T, ...]overtyping.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.