Skip to content

fix(core): correctly parse DEMO_MODE as boolean from env var.#5676

Merged
Soulter merged 2 commits intoAstrBotDevs:masterfrom
L-1ngg:fix/demo-mode-env-parsing
Mar 3, 2026
Merged

fix(core): correctly parse DEMO_MODE as boolean from env var.#5676
Soulter merged 2 commits intoAstrBotDevs:masterfrom
L-1ngg:fix/demo-mode-env-parsing

Conversation

@L-1ngg
Copy link
Contributor

@L-1ngg L-1ngg commented Mar 2, 2026

Fixes incorrect DEMO_MODE evaluation when it is configured via environment variables.
Previously, values like DEMO_MODE=False may be treated as truthy in if DEMO_MODE: checks (because non-empty strings are truthy in Python), causing demo-only logic to run unexpectedly. This change normalizes DEMO_MODE to a real boolean.

Modifications / 改动点

  • astrbot/core/__init__.py: Parse DEMO_MODE from env var into a boolean (e.g., only "true", "1", "t" enable it;
    otherwise it is False by default).

  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果

  1. Set DEMO_MODE=False and confirm it is evaluated as False:
    • PowerShell:
      • $env:DEMO_MODE="False"
      • uv run python -c "from astrbot.core import DEMO_MODE; print('DEMO_MODE=', DEMO_MODE); assert DEMO_MODE is False"
image
  1. Set DEMO_MODE=True and confirm it is evaluated as True:
    • PowerShell:
      • $env:DEMO_MODE="True"
      • uv run python -c "from astrbot.core import DEMO_MODE; print('DEMO_MODE=', DEMO_MODE); assert DEMO_MODE is True"
image

但是在该 debug 之前,就会导致被 Assert 掉


Checklist / 检查清单

  • 😊 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。/ If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
  • 👀 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。/ My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
  • 🤓 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到了 requirements.txtpyproject.toml 文件相应位置。/ 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.txt and pyproject.toml.
  • 😮 我的更改没有引入恶意代码。/ My changes do not introduce malicious code.

Summary by Sourcery

Bug Fixes:

  • 修复通过环境变量配置时 DEMO_MODE 真值判断不正确的问题,避免常见的“类似 false 的字符串”被错误地视为已启用。
Original summary in English

Summary by Sourcery

Bug Fixes:

  • Fix incorrect DEMO_MODE truthiness when configured via environment variables so that common false-like strings are no longer treated as enabled.

@dosubot dosubot bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Mar 2, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses an issue where the DEMO_MODE configuration, when set via environment variables, was not consistently interpreted as a boolean. The change ensures that DEMO_MODE is always a true boolean, preventing unintended execution of demo-specific logic and improving the reliability of feature toggling based on this variable.

Highlights

  • Boolean Parsing for DEMO_MODE: The DEMO_MODE environment variable is now correctly parsed as a boolean, ensuring that values like 'False' are not treated as truthy due to Python's string evaluation.
Changelog
  • astrbot/core/init.py
    • Modified the DEMO_MODE assignment to explicitly convert the environment variable string into a boolean value, considering 'true', '1', or 't' as True, and all other values (including 'False') as False.
Activity
  • The pull request was created by L-1ngg.
  • The author provided detailed motivation, modifications, and verification steps including screenshots for testing the DEMO_MODE evaluation.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - 我发现了 1 个问题,并给出了一些整体反馈:

  • 建议在调用 .lower() 之前先去除空白字符(例如使用 .strip().lower()),这样像 DEMO_MODE=" true " 这样的值也能被正确解析。
  • 如果你打算解析更多类似布尔值的字符串(例如 yes/noon/off),把这类逻辑集中到一个小的辅助函数中可能会更简洁(例如 parse_bool_env("DEMO_MODE", default=False)),并在其他类似标志位上复用。
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider stripping whitespace before the `.lower()` call (e.g., `.strip().lower()`) so values like `DEMO_MODE=" true "` are still parsed correctly.
- If you expect to parse more boolean-like values (e.g., `yes/no`, `on/off`), it might be cleaner to centralize this in a small helper (e.g., `parse_bool_env("DEMO_MODE", default=False)`) and reuse it for similar flags.

## Individual Comments

### Comment 1
<location path="astrbot/core/__init__.py" line_range="17" />
<code_context>
 os.makedirs(get_astrbot_data_path(), exist_ok=True)

-DEMO_MODE = os.getenv("DEMO_MODE", False)
+DEMO_MODE = os.getenv("DEMO_MODE", "False").lower() in ("true", "1", "t")

 astrbot_config = AstrBotConfig()
</code_context>
<issue_to_address>
**suggestion (bug_risk):** Consider normalizing/stripping the env var value before the truthiness check.

If `DEMO_MODE` is set with trailing spaces or newlines (e.g., from env files), the current check will treat it as `False` even when it visually looks like `true`. Adding `.strip()` before `.lower()` makes this more robust, e.g. `os.getenv("DEMO_MODE", "False").strip().lower() in ("true", "1", "t")`, while preserving the current semantics.

```suggestion
DEMO_MODE = os.getenv("DEMO_MODE", "False").strip().lower() in ("true", "1", "t")
```
</issue_to_address>

Sourcery 对开源项目是免费的——如果你觉得我们的评审有帮助,欢迎分享 ✨
帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据你的反馈改进后续评审。
Original comment in English

Hey - I've found 1 issue, and left some high level feedback:

  • Consider stripping whitespace before the .lower() call (e.g., .strip().lower()) so values like DEMO_MODE=" true " are still parsed correctly.
  • If you expect to parse more boolean-like values (e.g., yes/no, on/off), it might be cleaner to centralize this in a small helper (e.g., parse_bool_env("DEMO_MODE", default=False)) and reuse it for similar flags.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider stripping whitespace before the `.lower()` call (e.g., `.strip().lower()`) so values like `DEMO_MODE=" true "` are still parsed correctly.
- If you expect to parse more boolean-like values (e.g., `yes/no`, `on/off`), it might be cleaner to centralize this in a small helper (e.g., `parse_bool_env("DEMO_MODE", default=False)`) and reuse it for similar flags.

## Individual Comments

### Comment 1
<location path="astrbot/core/__init__.py" line_range="17" />
<code_context>
 os.makedirs(get_astrbot_data_path(), exist_ok=True)

-DEMO_MODE = os.getenv("DEMO_MODE", False)
+DEMO_MODE = os.getenv("DEMO_MODE", "False").lower() in ("true", "1", "t")

 astrbot_config = AstrBotConfig()
</code_context>
<issue_to_address>
**suggestion (bug_risk):** Consider normalizing/stripping the env var value before the truthiness check.

If `DEMO_MODE` is set with trailing spaces or newlines (e.g., from env files), the current check will treat it as `False` even when it visually looks like `true`. Adding `.strip()` before `.lower()` makes this more robust, e.g. `os.getenv("DEMO_MODE", "False").strip().lower() in ("true", "1", "t")`, while preserving the current semantics.

```suggestion
DEMO_MODE = os.getenv("DEMO_MODE", "False").strip().lower() in ("true", "1", "t")
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@dosubot
Copy link

dosubot bot commented Mar 2, 2026

Related Documentation

Checked 1 published document(s) in 1 knowledge base(s). No updates required.

How did I do? Any feedback?  Join Discord

@dosubot dosubot bot added the area:core The bug / feature is about astrbot's core, backend label Mar 2, 2026
fix(core): 添加.strip()以确保代码健壮性

Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request addresses an issue with parsing the DEMO_MODE environment variable, where string values like "False" were evaluated as truthy. The new implementation correctly converts the environment variable to a boolean. I have provided one suggestion to further improve the robustness of this boolean parsing by including more commonly used truthy string values.

Copy link
Member

@Dt8333 Dt8333 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

问题的确存在。解决方案没问题。
(不过……应该没人设置DEMO_MODE为非TRUE的值吧。为什么不直接删了呢?)

感觉Gemini提供的修改意见似乎也没问题。(虽说疑似过度全面了)

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Mar 3, 2026
@Soulter Soulter merged commit fa4df0b into AstrBotDevs:master Mar 3, 2026
6 checks passed
@astrbot-doc-agent
Copy link

No docs changes were generated in this run (docs repo had no updates).

Docs repo: AstrBotDevs/AstrBot-docs
Trigger: PR merged


AI change summary (not committed):


Experimental bot notice:

  • This output is generated by AstrBot-Doc-Agent for review only.
  • It does not represent the final documentation form.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core The bug / feature is about astrbot's core, backend lgtm This PR has been approved by a maintainer size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants