
python-conventions
by narumiruna
SKILL.md
name: python-conventions description: Enforce Python 3.12+ coding conventions when editing or creating Python code. Use for tasks that touch .py files, focusing on style, naming, typing, error handling, and performance-oriented idioms.
Python Conventions
Overview
Apply consistent Python conventions for code edits and new code. These conventions ensure readability, maintainability, and consistency across Python 3.12+ projects. Prefer existing project patterns unless they conflict with the conventions below.
For complete project workflows including tooling setup, see the python-project skill.
Core Conventions
Language Features:
- Use Python 3.12+ features and standard library first
- Use built-in generics:
list[X],dict[K, V],X | Yfor unions - Use
match/caseand walrus operator (:=) only when they improve readability
Code Quality:
- Follow PEP 8 for style and layout (see
references/pep8.mdfor summary) - Write descriptive names and single-responsibility functions
- Prefer readability and simplicity; follow "The Zen of Python" (
import this)
Modern Python Idioms:
- Use
pathlib.Pathinstead ofos.pathfor file operations - Use f-strings for string formatting
- Use context managers (
withstatements) for resource handling - Prefer
dataclassesor Pydantic for data models - Use
isinstance(x, int | float)instead ofisinstance(x, (int, float))(UP038)
Documentation:
- Use Google- or NumPy-style docstrings for public APIs
- Write comments about "why" rather than "what"
Error Handling:
- Use specific exception types; avoid bare
except - Create custom exceptions for domain errors
- Include enough context to debug without leaking secrets
Style Guidelines
Indentation and Line Length:
- Indent with 4 spaces; never use tabs
- Keep lines at or under 79 characters (or project standard if different)
- Use implicit line continuation in parentheses/brackets/braces
Naming Conventions:
- Variables and functions:
snake_case - Classes:
CamelCase - Constants:
UPPER_CASE_WITH_UNDERSCORES - Private attributes: prefix with single underscore
_private
Import Organization:
- One import per line (except
from ... import ...forms) - Order: standard library, third-party, local application
- Separate groups with blank lines
- Example:
import os from pathlib import Path import requests from loguru import logger from myapp.utils import helper
Whitespace and Layout:
- Two blank lines around top-level functions and classes
- One blank line around class methods
- Use blank lines sparingly within functions for logical sections
- Avoid extraneous whitespace inside brackets or before commas
Type Hints
When to Add Types:
- Always add type hints for new code
- Add types when updating existing interfaces
- Use
from __future__ import annotationsfor forward references
Modern Type Syntax (Python 3.12+):
def process_items(items: list[str], limit: int | None = None) -> dict[str, int]:
"""Process items and return counts."""
result: dict[str, int] = {}
for item in items[:limit]:
result[item] = result.get(item, 0) + 1
return result
Key Patterns:
- Use
list[X],dict[K, V],set[X]instead ofList,Dict,Set - Use
X | Y | Noneinstead ofOptional[X]orUnion[X, Y] - Use
typealiases for complex types:UserID = int | str - Use
Protocolfor structural subtyping when appropriate
Performance Guidelines
Memory Efficiency:
- Use generators and iterators for large datasets
- Prefer
itertoolsandfunctoolsfor common patterns - Use comprehensions for simple transformations
I/O Operations:
- Use
async/awaitfor I/O-bound workloads - Use context managers to ensure resource cleanup
- Consider connection pooling for repeated operations
When Performance Matters:
- Profile hot paths before optimizing
- Use appropriate data structures (
setfor membership,dictfor lookups) - Consider
lru_cachefor expensive pure functions
When to Read References
- PEP 8 details: See
references/pep8.mdfor comprehensive style guide - Project setup: See
python-projectskill for tooling and workflows
スコア
総合スコア
リポジトリの品質指標に基づく評価
SKILL.mdファイルが含まれている
ライセンスが設定されている
100文字以上の説明がある
GitHub Stars 100以上
3ヶ月以内に更新がある
10回以上フォークされている
オープンIssueが50未満
プログラミング言語が設定されている
1つ以上のタグが設定されている
レビュー
レビュー機能は近日公開予定です