AGENT SKILL
python
Python best practices with type hints, structure, and testing conventions
- License 许可
- MIT
- 最近更新
- 2026-11-05
- Stars
- ★ 2
- Forks
- 0
01 前言 Front Matter
- name
- python
- license
- MIT
- metadata
- { "author": "shahboura", "version": "2.0.0", "audience": "developers", "workflow": "development" }
- description
- Python best practices with type hints, structure, and testing conventions
- compatibility
- opencode
02 资源文件 1
- 参考文档agents/openai.yaml269 B
03 SKILL.md 正文
What I do
- Enforce type hints and docstring standards on all Python code
- Guide proper async patterns, resource management, and error handling
- Apply naming conventions and import organization rules
When to use me
Use this when working on Python projects that require strict typing and code quality standards.
Key Rules
- Type hints are REQUIRED on ALL function signatures — no exceptions
- Use Google-style docstrings for all public APIs (Args, Returns, Raises sections)
- Always use context managers (
with) for file handles, DB sessions, and resources - Prefer list/dict comprehensions over equivalent for-loops
- Use
async/awaitfor I/O-bound operations where async libraries are available; preferasyncio.TaskGroup(3.11+) for structured concurrency - Prefer typed data models (
@dataclass, Pydantic models, orTypedDict) as appropriate for domain boundaries - Import order: standard library → third-party → local application (blank line between groups)
- Use
pathlib.Pathoveros.pathfor file operations - Prefer
Optional[T](orT | Noneon 3.10+) — never leave nullable returns untyped - Custom context managers via
@contextmanagerfor resource lifecycle patterns - Use
pytestfixtures with type annotations; for async tests, use the project's async test setup (for example@pytest.mark.asynciowithpytest-asyncio) - Use a verify-fix-verify loop: run the validation commands below, fix any failures, and rerun until all checks pass
Naming Conventions
snake_casefor functions/variables/modulesPascalCasefor classes and exceptionsUPPER_SNAKE_CASEfor constants- Prefix internal helpers with
_when they are not part of public API
Common Pitfalls
- Unannotated public APIs that silently degrade type safety
- Resource leaks from missing context managers (
with) - Overusing broad
except Exceptionwithout preserving context - Async functions called without awaiting in tests or orchestration logic
Example Patterns
from dataclasses import dataclass
from pathlib import Path
@dataclass
class User:
id: int
email: str
def load_email(path: Path) -> str:
with path.open("r", encoding="utf-8") as handle:
return handle.read().strip()
Validation Commands
Prefer project scripts when present; use defaults below otherwise.
ruff check .
ruff format .
mypy .
pytest