fix: validate bool config values and add reasoning field schema - #9689
Open
Rain-0x01-39 wants to merge 2 commits into
Open
fix: validate bool config values and add reasoning field schema#9689Rain-0x01-39 wants to merge 2 commits into
Rain-0x01-39 wants to merge 2 commits into
Conversation
Contributor
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- In
validate()the bool branch assignsdata[key] = castedeven whencasted is None, which both masks the original value and may introduceNoneinto the config; consider only updatingdata[key]when casting succeeds.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In `validate()` the bool branch assigns `data[key] = casted` even when `casted is None`, which both masks the original value and may introduce `None` into the config; consider only updating `data[key]` when casting succeeds.
## Individual Comments
### Comment 1
<location path="astrbot/dashboard/services/config_service.py" line_range="285-284" />
<code_context>
)
data[key] = casted
elif meta["type"] == "bool" and not isinstance(value, bool):
- errors.append(
- f"错误的类型 {path}{key}: 期望是 bool, 得到了 {type(value).__name__}",
- )
+ casted = try_cast(value, "bool")
+ if casted is None:
+ errors.append(
+ f"错误的类型 {path}{key}: 期望是 bool, 得到了 {type(value).__name__}",
+ )
+ data[key] = casted
elif meta["type"] in ["string", "text"] and not isinstance(value, str):
errors.append(
</code_context>
<issue_to_address>
**issue (bug_risk):** Avoid assigning `data[key] = casted` when casting fails and returns `None`.
In the `bool` branch, `data[key]` is updated even when `casted` is `None`, so invalid values are both logged as errors and overwritten with `None`. This differs from other types, where the original value is kept on failure. Please only update `data[key]` when casting succeeds, e.g.:
```python
data[key] = casted if casted is not None else value
```
or skip assignment when `casted` is `None`.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| @@ -276,9 +283,12 @@ def validate(data: dict, metadata: dict = schema, path="") -> None: | |||
| ) | |||
| data[key] = casted | |||
Contributor
There was a problem hiding this comment.
issue (bug_risk): Avoid assigning data[key] = casted when casting fails and returns None.
In the bool branch, data[key] is updated even when casted is None, so invalid values are both logged as errors and overwritten with None. This differs from other types, where the original value is kept on failure. Please only update data[key] when casting succeeds, e.g.:
data[key] = casted if casted is not None else valueor skip assignment when casted is None.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
reasoning字段缺少 schema 定义,Dashboard 无法正确展示而是降级为文本框。Provider config 中 bool 类型字段在 WebUI 保存时可能以字符串形式传入("true"/"false"),当前validate()会直接报类型错误而不是尝试转换。Modifications / 改动点
default.py: 添加reasoning字段 schema(bool 类型)config_service.py:try_cast()新增 bool 转换,validate()bool 校验改为先尝试转换再报错Dashboard i18n: 添加
reasoning字段翻译(en/zh/ru)This is NOT a breaking change. / 这不是一个破坏性变更。
Screenshots or Test Results / 运行截图或测试结果
Checklist / 检查清单
😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
/ 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。
👀 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
Validate boolean provider config values more robustly and expose the model reasoning capability flag in configuration metadata and dashboard UI.
New Features:
Bug Fixes:
Enhancements: