feat(cli): derive CLI version from core config#8666
Conversation
There was a problem hiding this comment.
Code Review
This pull request replaces the hardcoded version string in astrbot/cli/__init__.py by importing VERSION from astrbot.core.config.default. Feedback highlights that this direct import introduces significant performance overhead and potential side effects during CLI startup due to the size and execution of default.py. It is recommended to either extract the version to a lightweight file or use lazy loading via __getattr__.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| from astrbot.core.config.default import VERSION | ||
|
|
||
| __version__ = VERSION |
There was a problem hiding this comment.
问题分析
直接在 astrbot/cli/__init__.py 中导入 astrbot.core.config.default 会引入较大的开销和潜在的副作用:
- 性能开销:
default.py是一个超过 4300 行的大文件,包含大量的配置字典和元数据。每次导入astrbot.cli(例如执行 CLI 命令、获取帮助信息或进行包元数据解析)都需要解析和执行这 4300+ 行代码,这会显著增加 CLI 的启动延迟。 - 导入期副作用:
default.py在导入时会执行get_astrbot_data_path()并初始化DB_PATH。如果在某些受限环境(如构建、打包或测试环境)中直接导入astrbot.cli,可能会因为路径解析或权限问题导致导入失败。
解决方案建议
方案一:架构重构(推荐)
将 VERSION 常量提取到一个轻量级的专用文件(例如 astrbot/version.py)中,然后让 default.py 和 cli/__init__.py 都从该文件导入。
方案二:延迟加载(Lazy Loading)
如果不想调整文件结构,可以利用 Python 3.7+ 支持的模块级 __getattr__ 来延迟导入 VERSION,只有在真正访问 __version__ 属性时才触发对 default.py 的导入。
| from astrbot.core.config.default import VERSION | |
| __version__ = VERSION | |
| def __getattr__(name: str) -> str: | |
| if name == "__version__": | |
| from astrbot.core.config.default import VERSION | |
| globals()["__version__"] = VERSION | |
| return VERSION | |
| raise AttributeError(f"module '{__name__}' has no attribute '{name}'") |
Modifications / 改动点
This PR makes the CLI package version derive from the core version constant instead of keeping a second hard-coded value.
本 PR 让 CLI 包版本从 core 版本常量派生,避免维护第二份硬编码版本号。
astrbot.cli.__version__ = "4.25.3"withastrbot.core.config.default.VERSION.astrbot.cli.__version__as the public attribute used by the CLI.pyproject.toml/default.VERSIONwhile leaving the CLI version behind.Screenshots or Test Results / 运行截图或测试结果
Verification:
Duplicate check:
Checklist / 检查清单
😊 No user-facing feature is added; this checklist item is N/A.
/ 未新增面向用户的功能;此项不适用。
👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
/ 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。
🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in
requirements.txtandpyproject.toml./ 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到
requirements.txt和pyproject.toml文件相应位置。😮 My changes do not introduce malicious code.
/ 我的更改没有引入恶意代码。
Summary by Sourcery
Enhancements: