python-standards
Python code quality standards (PEP 8, type hints, docstrings). Use when writing Python code.
安装与使用
请根据来源仓库和你的 AI 工具说明安装此技能。先阅读技能文件和依赖要求,再添加到工作流。
查看来源 ↗使用指南技能说明
Python Standards Skill
Python code quality standards for autonomous-dev project.
See: code-examples.md for complete code examples See: templates.md for organization templates
When This Activates
- Writing Python code
- Code formatting
- Type hints
- Docstrings
- Keywords: "python", "format", "type", "docstring"
Code Style (PEP 8 + Black)
| Setting | Value |
|---|---|
| Line length | 100 characters |
| Indentation | 4 spaces (no tabs) |
| Quotes | Double quotes |
| Imports | Sorted with isort |
black --line-length=100 src/ tests/
isort --profile=black --line-length=100 src/ tests/
Type Hints (Required)
Rule: All public functions must have type hints on parameters and return.
def process_file(
input_path: Path,
output_path: Optional[Path] = None,
*,
max_lines: int = 1000
) -> Dict[str, any]:
"""Type hints on all parameters and return."""
pass
See: code-examples.md#type-hints for complete examples
Docstrings (Google Style)
Rule: All public functions/classes need docstrings with Args, Returns, Raises.
def process_data(data: List[Dict], *, batch_size: int = 32) -> ProcessResult:
"""Process data with validation.
Args:
data: Input data as list of dicts
batch_size: Items per batch (default: 32)
Returns:
ProcessResult with items and metrics
Raises:
ValueError: If data is empty
"""
See: code-examples.md#docstrings-google-style
Error Handling
Rule: Error messages must include context + expected + docs link.
# ✅ GOOD
raise FileNotFoundError(
f"Config file not found: {path}\n"
f"Expected: YAML with keys: model, data\n"
f"See: docs/guides/configuration.md"
)
# ❌ BAD
raise FileNotFoundError("File not found")
See: code-examples.md#error-handling
Naming Conventions
| Type | Convention | Example |
|---|---|---|
| Classes | PascalCase | ModelTrainer |
| Functions | snake_case | train_model() |
| Constants | UPPER_SNAKE | MAX_LENGTH |
| Private | _underscore | _helper() |
Best Practices
-
Keyword-only args - Use
*for clarity -
Pathlib - Use
Pathnot string paths -
Context managers - Use
withfor resources - Dataclasses - For configuration objects
# Keyword-only args
def train(data: List, *, learning_rate: float = 1e-4):
pass
# Pathlib
config = Path("config.yaml").read_text()
See: code-examples.md#best-practices
Code Quality Commands
flake8 src/ --max-line-length=100 # Linting
mypy src/[project_name]/ # Type checking
pytest --cov=src --cov-fail-under=80 # Coverage
Key Takeaways
- Type hints - Required on all public functions
- Docstrings - Google style, with Args/Returns/Raises
- Black formatting - 100 char line length
- isort imports - Sorted and organized
- Helpful errors - Context + expected + docs link
- Pathlib - Use Path not string paths
-
Keyword args - Use
*for clarity - Dataclasses - For configuration objects
Related Files
- code-examples.md - Complete Python code examples
- templates.md - File organization templates
使用示例
查看来源仓库中的使用示例与配置说明。
查看来源 ↗来源历史
- 收录时间
- 2026-01-20
- 索引更新时间
- 2026-01-26
- Branch
- main
收录与同步日期属于本站记录。源码修改历史请查看来源仓库。
GitHub ↗chat Comments (0)
Sign in to join the discussion and leave a comment.
No comments yet. Be the first to share your thoughts!