4.1 KiB
4.1 KiB
Python Reference
Use this file only when deeper guidance is needed. SKILL.md is the default operating contract.
When To Consult This File
- You are choosing between multiple Python design options.
- You need stronger guidance for typing, validation, async, or testing.
- You are creating new modules or new project structure rather than making a local edit.
- You are reviewing code and want a more complete checklist.
Stronger Typing Guidance
- Prefer
collections.abcandtypingabstractions such asSequence,Mapping,Callable, andIterator. - Use
Protocolfor behavioral contracts when inheritance is not required. - Use
TypedDictfor structured dictionary payloads when a dataclass or Pydantic model is not appropriate. - Use
Finalfor constants andClassVarfor class-level attributes when helpful. - Keep
Anyrare, local, and justified. - If the project supports it, use
@overrideon overriding methods.
Architecture Heuristics
- Domain layer: pure business rules, no framework or persistence code.
- Repository or data layer: database and external service access.
- Service or use-case layer: orchestration across domain rules and collaborators.
- Interface layer: HTTP, CLI, events, serialization, request and response shaping.
- Infrastructure layer: configuration, logging setup, DB sessions, clients.
Use this split when it fits the repository. Do not impose it mechanically on smaller scripts or simpler codebases.
Data Modeling
- Use Pydantic for request payloads, config, and data from untrusted sources.
- Use dataclasses for internal value objects and simple structured state.
- Use
frozen=Truefor values that should not change after creation. - Use
slots=Truewhere the codebase already prefers it or where many instances are created.
Error Handling
- Prefer project-specific exception types over generic
ValueErrororRuntimeErrorwhen a domain concept exists. - Wrap lower-level exceptions with domain context when crossing layers.
- Keep exception handling near boundaries unless recovery is local and explicit.
Logging
- Prefer structured logging when the project supports it.
- Log important operational events at service or interface boundaries.
- Do not log secrets, credentials, or sensitive user data.
- Avoid
print()for operational behavior unless the program is explicitly a simple script or CLI output path.
Async Guidance
- Use async for I/O-bound workflows, not CPU-bound work.
- Keep blocking calls out of coroutines; use thread or process offloading where appropriate.
- Prefer
asyncio.TaskGroupover ad hoc task orchestration when available. - Be explicit about sync-to-async boundaries.
Security Guidance
- Never hardcode secrets or credentials.
- Validate all external input before it enters core logic.
- Avoid dangerous calls unless explicitly justified and safely constrained:
eval,execpickle.loadsyaml.loadsubprocesswithshell=Trueos.system
- Do not interpolate raw user input into SQL, shell commands, or file-system-sensitive operations.
Testing Guidance
- Update tests whenever behavior changes.
- Prefer unit tests for pure logic and integration tests for boundaries.
- Use parametrization to improve coverage without duplicating setup.
- Test behavior and observable outcomes rather than implementation details.
- Mock external boundaries selectively; avoid mocking the core logic you actually want to verify.
Review Heuristics
When reviewing Python changes, prioritize:
- correctness and regressions
- incomplete or misleading typing
- invalid assumptions at trust boundaries
- misplaced logic between layers
- unnecessary abstraction or indirection
- missing tests for changed behavior
- risky calls, secret handling, or unsafe subprocess use
Common Verification Flow
If the repository uses uv, the usual flow is:
uv run ruff format <paths>uv run ruff check --fix <paths>uv run ruff check <paths>uv run mypy <paths-or-package>uv run pytest <relevant tests>
If the repository uses different commands or tools, follow the repository.