Skip to content

fix(core): enforce Python 3.12 floor in the startup environment check - #10192

Open
iuiu-py wants to merge 1 commit into
AstrBotDevs:masterfrom
iuiu-py:fix/check-env-python-312
Open

iuiu-py wants to merge 1 commit into
AstrBotDevs:masterfrom
iuiu-py:fix/check-env-python-312

Conversation

@iuiu-py

@iuiu-py iuiu-py commented Sep 22, 2026

Copy link
Copy Markdown

pyproject.toml declares requires-python = ">=3.12", but check_env()
still admitted Python 3.10, and WebUI-driven upgrades bypass pip's
requires-python enforcement entirely. A 3.10 install therefore kept
booting until a plugin or code path using 3.12 syntax exploded at
runtime.

Raise the runtime check to match pyproject and cover 3.10/3.11 with
the regression test.

Fixes #9945

Testing

  • tests/test_main.py::test_check_env now covers three cases: 3.12 passes, 3.10 exits, 3.9 exits. The 3.10 case failed with DID NOT RAISE SystemExit on unpatched master (95e98b8) and passes with the fix; the full file passes 18/18.
  • ruff format --check and ruff check clean on both touched files.

Why (context)

Users who deployed on Python 3.10 before the 3.12 requirement and have been upgrading through the WebUI never hit pip's requires-python gate (WebUI upgrades replace code, not the interpreter), so their instance boots until a plugin or dependency using 3.12 syntax crashes. The startup check is the only place the floor can be enforced at runtime.

Summary by Sourcery

Align the startup Python version check with the project’s declared Python 3.12 requirement.

Bug Fixes:

  • Enforce the Python 3.12 minimum at startup so installations upgraded outside pip cannot run on unsupported Python versions.

Tests:

  • Extend the environment-check regression coverage to verify Python 3.12 passes while Python 3.10 and 3.9 are rejected.

pyproject.toml declares requires-python = ">=3.12", but check_env()
still admitted Python 3.10, and WebUI-driven upgrades bypass pip's
requires-python enforcement entirely. A 3.10 install therefore kept
booting until a plugin or code path using 3.12 syntax exploded at
runtime.

Raise the runtime check to match pyproject and cover 3.10/3.11 with
the regression test.

Fixes AstrBotDevs#9945

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey - I've found 2 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="main.py" line_range="66" />
<code_context>
+    # Keep this aligned with pyproject.toml's `requires-python = ">=3.12"`.
+    # WebUI upgrades skip pip's requires-python enforcement, so this runtime
+    # check is the only guard for installs that predate the 3.12 requirement.
+    if not (sys.version_info.major == 3 and sys.version_info.minor >= 12):
+        logger.error("Please run this project with Python 3.12 or later.")
         exit()
</code_context>
<issue_to_address>
**issue (broader_impact):** The documented `astrbot run` console entry point never calls `check_env()`; it invokes `astrbot.cli.commands.cmd_run.run()` instead. Consequently, a WebUI or console-script startup on Python 3.10 or 3.11 bypasses this new floor check and can still boot.

**Triggers:** When AstrBot is started with the installed `astrbot run` command rather than by executing `main.py` directly.

**Suggested fix:** Invoke `check_env()` from the console-script startup path before launching `run_astrbot()`, or centralize startup through a single entry point.
</issue_to_address>

### Comment 2
<location path="main.py" line_range="66" />
<code_context>
+    # Keep this aligned with pyproject.toml's `requires-python = ">=3.12"`.
+    # WebUI upgrades skip pip's requires-python enforcement, so this runtime
+    # check is the only guard for installs that predate the 3.12 requirement.
+    if not (sys.version_info.major == 3 and sys.version_info.minor >= 12):
+        logger.error("Please run this project with Python 3.12 or later.")
         exit()
</code_context>
<issue_to_address>
**nitpick (bug_risk):** The condition rejects every Python with a major version other than 3, including Python 4, even though `requires-python = ">=3.12"` and the error message say Python 3.12 or later is supported. A Python 4 installation that satisfies the declared requirement exits before startup.

**Triggers:** When the project is run under Python 4 or a later major version.

**Suggested fix:** Use `if sys.version_info < (3, 12):` unless Python 4 is intentionally unsupported and the project metadata is updated with an upper bound.

```suggestion
    if sys.version_info < (3, 12):
```
</issue_to_address>

Sourcery assessment

Needs a human reviewer. 1 finding to address first, and if the Python floor is raised incorrectly or downstream users still depend on Python 3.10 or 3.11, the startup guard will prevent the application from launching for those users. Reverting restores compatibility, but any outage or failed startup attempts during the interval are not undone.

Blocking findings: main.py:66


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread main.py
# Keep this aligned with pyproject.toml's `requires-python = ">=3.12"`.
# WebUI upgrades skip pip's requires-python enforcement, so this runtime
# check is the only guard for installs that predate the 3.12 requirement.
if not (sys.version_info.major == 3 and sys.version_info.minor >= 12):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

issue (broader_impact): The documented astrbot run console entry point never calls check_env(); it invokes astrbot.cli.commands.cmd_run.run() instead. Consequently, a WebUI or console-script startup on Python 3.10 or 3.11 bypasses this new floor check and can still boot.

Triggers: When AstrBot is started with the installed astrbot run command rather than by executing main.py directly.

Suggested fix: Invoke check_env() from the console-script startup path before launching run_astrbot(), or centralize startup through a single entry point.

Comment thread main.py
# Keep this aligned with pyproject.toml's `requires-python = ">=3.12"`.
# WebUI upgrades skip pip's requires-python enforcement, so this runtime
# check is the only guard for installs that predate the 3.12 requirement.
if not (sys.version_info.major == 3 and sys.version_info.minor >= 12):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nitpick (bug_risk): The condition rejects every Python with a major version other than 3, including Python 4, even though requires-python = ">=3.12" and the error message say Python 3.12 or later is supported. A Python 4 installation that satisfies the declared requirement exits before startup.

Triggers: When the project is run under Python 4 or a later major version.

Suggested fix: Use if sys.version_info < (3, 12): unless Python 4 is intentionally unsupported and the project metadata is updated with an upper bound.

Suggested change
if not (sys.version_info.major == 3 and sys.version_info.minor >= 12):
if sys.version_info < (3, 12):

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] pyproject.toml 约束了却仍能用旧版本 Python

1 participant