Python code style for fund-portfolio-bot
This Skill emphasizes the most critical and easily overlooked rules in coding conventions.
See Section 3 (Core Constraints) of
for the complete coding conventions.
See
.claude/skills/architecture/SKILL.md
for layered architecture constraints.
When to use
Use this Skill in the following scenarios (trigger words: code style, type hints, docstring, precision, code style, type hints, docstring):
- Generating new Python modules (especially under )
- Modifying existing functions or classes
- Conducting code refactoring or code reviews
- When users mention "type hints", "Decimal", "documentation", or "code conventions"
Type and Numeric Correctness
- All function parameters and return values should have type hints added.
- Prefer modern type syntax:
- , ,
- Avoid outdated syntax like / unless there is an existing unified convention in the project.
- Financial-related fields such as amount, net value, and shares must use .
- Do not use for any financial calculations.
Docstring and Comments
- Public classes and functions should have concise Chinese docstrings explaining:
- Main responsibilities
- Key business constraints or considerations
- Docstrings do not need to repeat type information (types are based on annotations).
- Numbered Tag Comments (CLI layer convention):
- Mark logical steps inside functions with
- Example: → →
- Only add comments to explain non-intuitive business rules; avoid noisy comments.
Module and Class Internal Structure
Principle: Entry points at the top, utilities at the bottom; public members at the top, private members at the bottom.
Class internal order:
- Public methods
- Private helper methods starting with
Module internal order:
- Imports (grouped by standard library / third-party / local)
- Public classes and public functions
- Private utility functions only used within the module (e.g., )
Naming Conventions
- State class fields or enum values use lowercase strings, e.g.:
- File names, function names, variable names:
- Class names:
- CLI Layer Function Naming (unified convention for v0.4.2+):
- : Parameter parsing function
- : Output formatting helper functions (e.g., , )
- : Command execution functions (e.g., , , )
- : CLI main entry point, only handles routing
Layered and Configuration-related Constraints
- Code in the layer should not import from or .
- Avoid direct use of in business logic:
- Prioritize obtaining configurations through existing configuration modules or adaptation layers.
DCA & AI Division of Labor Naming Conventions
This project is an AI-driven investment tool. In code related to DCA, historical scanning, and AI analysis, strictly follow the division principle of "Rules calculate facts, AI provides explanations", and reinforce this boundary through naming.
Rule Layer Data Models
The rule layer only outputs recalculable structured facts; subjective conclusions are strictly prohibited.
| Suffix | Definition | Intra-module Example | Cross-module Example |
|---|
| Structured fact snapshot (date, amount, interval, etc.) | | |
| Rule validation result (hit + deviation + explanation) | | |
| Exception marker (no conclusion, only marking) | | |
| Proposed solution (not stored, in-memory structure) | | |
| Internal intermediate aggregation (e.g., backfill result) | | |
| Report for CLI/AI display | | |
Simplification Principle: Omit the prefix when the module path already contains domain information; retain context when exporting across modules.
Flow Function Verbs
| Verb | Constraint | Intra-module Example | Cross-module Example |
|---|
| Read-only calculation, returns | | (batch as parameter) |
| Read-only, side-effect-free (idempotent) | | |
| Returns , not stored | | |
| Write operation, modifies database | | (no need for for_batch) |
Key Principles:
- Seeing / / means it is safe to call (read-only)
- Seeing means to be cautious as it modifies the database
- Use parameters rather than function names to express "what" (e.g., batch_id, fund_code are parameters)
AI Layer (Reserved)
AI generates semantic explanations based on
from the rule layer, only writes explanatory fields, and does not modify core data.
- : Insights (e.g., "This transaction may be limited by quota")
- : Natural language explanation
- : Classification label
Pre-submission Checks
Whenever possible:
- Run static checks (e.g., as configured in the project).
- Quickly review the current diff to confirm:
- Style cleanup does not change business behavior
- No debugging code is left behind (e.g., , ).