code-style
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePython code style for fund-portfolio-bot
Python code style for fund-portfolio-bot
本 Skill 强调编码规范中最关键、最容易被忽略的规则。
完整编码规范见第 3 节(核心约束)。 分层架构约束见CLAUDE.md。.claude/skills/architecture/SKILL.md
This Skill emphasizes the most critical and easily overlooked rules in coding conventions.
See Section 3 (Core Constraints) offor the complete coding conventions. SeeCLAUDE.mdfor layered architecture constraints..claude/skills/architecture/SKILL.md
When to use
When to use
在以下场景使用本 Skill(触发词:代码风格、类型注解、Docstring、精度、code style、type hints、docstring):
- 生成新的 Python 模块(尤其是 下)
src/ - 修改现有函数或类
- 做代码重构或代码评审
- 用户提到"类型注解"、"Decimal"、"文档"、"代码规范"时
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 )
src/ - 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
- 所有函数参数与返回值都应添加类型注解。
- 优先使用现代类型语法:
- ,
list[str],dict[str, Decimal]X | None - 避免使用 /
List等老式写法,除非项目已有统一约定。Optional
- 金额、净值、份额等金融相关字段一律使用 。
Decimal - 不要使用 做任何金融计算。
float
- All function parameters and return values should have type hints added.
- Prefer modern type syntax:
- ,
list[str],dict[str, Decimal]X | None - Avoid outdated syntax like /
Listunless there is an existing unified convention in the project.Optional
- Financial-related fields such as amount, net value, and shares must use .
Decimal - Do not use for any financial calculations.
float
Docstring 与注释
Docstring and Comments
- 公开的类与函数应该有简洁的中文 docstring,说明:
- 主要职责
- 关键业务约束或注意点
- Docstring 不需要重复类型信息(类型以注解为准)。
- 数字标签注释(CLI 层规范):
- 函数内部用
# 1.# 2.标记逻辑步骤# 3. - 示例:→
# 1. 解析参数→# 2. 调用 Flow# 3. 格式化输出
- 函数内部用
- 注释只在业务规则不直观时补充解释,避免噪音注释。
- 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
# 1.# 2.# 3. - Example: →
# 1. Parse parameters→# 2. Call Flow# 3. Format output
- Mark logical steps inside functions with
- Only add comments to explain non-intuitive business rules; avoid noisy comments.
模块与类内部结构
Module and Class Internal Structure
原则:入口在上,工具在下;公开在上,私有在下。
类内部顺序:
__init__- 公共方法
- 以 开头的私有辅助方法
_
模块内部顺序:
- import(按标准库 / 第三方 / 本地分组)
- 公共类与公共函数
- 仅模块内部使用的私有工具函数(例如 )
_helper_*
Principle: Entry points at the top, utilities at the bottom; public members at the top, private members at the bottom.
Class internal order:
__init__- 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., )
_helper_*
命名惯例
Naming Conventions
- 状态类字段或枚举值用小写字符串,例如:
- ,
"normal","delayed""pending"
- 文件名、函数名、变量名:
snake_case - 类名:
PascalCase - CLI 层函数命名(v0.4.2+ 统一规范):
- :参数解析函数
_parse_args() - :格式化输出辅助函数(如
_format_*(),_format_result())_format_fees() - :命令执行函数(如
_do_*(),_do_buy(),_do_list())_do_confirm() - :CLI 主入口,只做路由
main()
- State class fields or enum values use lowercase strings, e.g.:
- ,
"normal","delayed""pending"
- File names, function names, variable names:
snake_case - Class names:
PascalCase - CLI Layer Function Naming (unified convention for v0.4.2+):
- : Parameter parsing function
_parse_args() - : Output formatting helper functions (e.g.,
_format_*(),_format_result())_format_fees() - : Command execution functions (e.g.,
_do_*(),_do_buy(),_do_list())_do_confirm() - : CLI main entry point, only handles routing
main()
分层与配置相关约束
Layered and Configuration-related Constraints
- 层代码不要从
core或adapters导入。app - 业务逻辑中避免直接使用 :
os.getenv- 优先通过已有的配置模块或适配层获取配置。
- Code in the layer should not import from
coreoradapters.app - Avoid direct use of in business logic:
os.getenv- Prioritize obtaining configurations through existing configuration modules or adaptation layers.
DCA & AI 分工命名规范
DCA & AI Division of Labor Naming Conventions
本项目是 AI 驱动 的投资工具。在 DCA、历史扫描、AI 分析相关代码中,严格遵循 "规则算事实,AI 做解释" 的分工原则,通过命名来强化这个边界。
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
规则层只输出可重算的结构化事实,严禁直接生成主观结论。
| 后缀 | 定义 | 模块内示例 | 跨模块示例 |
|---|---|---|---|
| 结构化事实快照(日期、金额、间隔等) | | |
| 规则验证结果(命中+偏差+说明) | | |
| 异常标记(不下结论,仅标记) | | |
| 建议方案(不入库,内存结构) | | |
| 内部中间聚合(如回填结果) | | |
| CLI/AI 展示用报告 | | |
简化原则:模块路径已包含领域信息时,可省略前缀;跨模块导出时保留上下文。
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 函数动词
Flow Function Verbs
| 动词 | 约束 | 模块内示例 | 跨模块示例 |
|---|---|---|---|
| 只读计算,返回 | | |
| 只读无副作用(幂等) | | |
| 返回 | | |
| 写操作,修改数据库 | | |
关键原则:
- 看到 /
scan_/build_就知道安全可调(只读)draft_ - 看到 就要警惕会修改数据库
backfill_ - 参数而非函数名来表达"对什么"(batch_id, fund_code 等是参数)
| Verb | Constraint | Intra-module Example | Cross-module Example |
|---|---|---|---|
| Read-only calculation, returns | | |
| Read-only, side-effect-free (idempotent) | | |
| Returns | | |
| Write operation, modifies database | | |
Key Principles:
- Seeing /
scan_/build_means it is safe to call (read-only)draft_ - Seeing means to be cautious as it modifies the database
backfill_ - Use parameters rather than function names to express "what" (e.g., batch_id, fund_code are parameters)
AI 层(预留)
AI Layer (Reserved)
AI 基于规则层的 生成语义解释,仅写入解释性字段,不修改核心数据。
*Facts- :洞察(如"这笔交易可能是限额")
*Insight - :自然语言解释
*Explanation - :分类标签
*Label
AI generates semantic explanations based on from the rule layer, only writes explanatory fields, and does not modify core data.
*Facts- : Insights (e.g., "This transaction may be limited by quota")
*Insight - : Natural language explanation
*Explanation - : Classification label
*Label
提交前检查
Pre-submission Checks
在可能的情况下:
- 运行静态检查(例如项目中配置的 )。
ruff check --fix . - 快速浏览本次 diff,确认:
- 风格清理没有改变业务行为
- 没有遗留调试代码(例如 、
print)。breakpoint()
Whenever possible:
- Run static checks (e.g., as configured in the project).
ruff check --fix . - Quickly review the current diff to confirm:
- Style cleanup does not change business behavior
- No debugging code is left behind (e.g., ,
print).breakpoint()