Skip to content

fix(mem_scheduler): make EnvConfigMixin tolerate absent optional fields - #2425

Closed
simpleqt wants to merge 1 commit into
MemTensor:mainfrom
simpleqt:sq/env-config-optional
Closed

simpleqt wants to merge 1 commit into
MemTensor:mainfrom
simpleqt:sq/env-config-optional

Conversation

@simpleqt

Copy link
Copy Markdown

Description

Fixes #2424

EnvConfigMixin.from_env crashed with a bare raise ValueError() (empty message) whenever a field declared with default = None had no environment variable — so a config as ordinary as port: Optional[int] = None could not be built from the environment at all. This is on the scheduler init path (memos/configs/mem_scheduler.py → RabbitMQConfig.from_env()).

Implementation:

  • absent non-required fields are simply omitted from the kwargs so pydantic applies the model's own default (the old elif field_info.default is not None / else: raise ValueError() pair is gone)
  • _parse_env_value unwraps Optional[X] / X | None via typing.get_args, so an optional int/float env var parses as its inner type instead of staying a raw string

No dependency changes.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has Been Tested?

  • Unit Test

New tests/mem_scheduler/test_env_config_mixin.py covers: absent optional fields fall back to defaults (red/green discriminator — crashed with the empty ValueError on main), Optional[int] parsing to int and bool parsing, and that genuinely required fields still raise the named error.

  • Test Script Or Test Steps
poetry run pytest tests/mem_scheduler/test_env_config_mixin.py -v

ruff check --no-fix reports no new findings versus main on the touched source file (the six it lists there are pre-existing).

Checklist

  • I have performed a self-review of my own code | 我已自行检查了自己的代码
  • I have commented my code in hard-to-understand areas | 我已在难以理解的地方对代码进行了注释
  • I have added tests that prove my fix is effective or that my feature works | 我已添加测试以证明我的修复有效或功能正常
  • I have linked the issue to this PR (if applicable) | 我已将 issue 链接到此 PR(如果适用)

from_env crashed with a bare, message-less ValueError() when a field
declared with default None had no environment variable, so any config
with a plain optional field (e.g. RabbitMQConfig) could not be built
from the environment at all. Absent optional fields are now simply
omitted so pydantic applies the model default. Additionally,
_parse_env_value unwraps Optional[X] / X | None annotations so an
optional int or float env var parses as its inner type.

Red/green verified: the new tests fail against the old implementation
(empty ValueError) and pass with the fix.

Signed-off-by: simpleqt <89645338+simpleqt@users.noreply.github.com>
Copilot AI lite review requested due to automatic review settings September 28, 2026 14:01
@Memtensor-AI Memtensor-AI added area:scheduler 调度模块 status:in-progress Someone or AI is working on it | 人工或 AI 正在处理 labels Sep 28, 2026

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@Memtensor-AI

Copy link
Copy Markdown
Collaborator

🤖 Open Code Review

Target: PR #2425
Task: af001f99020e3d90
Base: main
Head: sq/env-config-optional

🔍 OpenCodeReview found 2 issue(s) in this PR.


1. src/memos/mem_scheduler/general_modules/misc.py (L84-L88)

The filter uses arg is not type(None) — this calls type(None) on every iteration, creating a new reference each time. While NoneType is a singleton so the identity check (is not) works correctly in CPython, it is more idiomatic and explicit to use arg is not type(None) replaced with arg is not NoneType via a pre-bound name, or simply compare with == (arg != type(None)). More importantly, because type(None) is computed fresh on each list-comprehension iteration, the intent is slightly obscured. Consider binding it once before the comprehension:

none_type = type(None)
if none_type in args:
    remaining = [arg for arg in args if arg is not none_type]

This is a minor style/clarity note — the current code is functionally correct in CPython.

💡 Suggested Change

Before:

        args = get_args(target_type)
        if type(None) in args:
            remaining = [arg for arg in args if arg is not type(None)]
            if len(remaining) == 1:
                target_type = remaining[0]

After:

        args = get_args(target_type)
        none_type = type(None)
        if none_type in args:
            remaining = [arg for arg in args if arg is not none_type]
            if len(remaining) == 1:
                target_type = remaining[0]

2. tests/mem_scheduler/test_env_config_mixin.py (L37)

The debug field is checked with is True only, while port is verified with both value equality and isinstance. The implementation returns value.lower() in (...) which is always a real bool, so is True is safe — but for consistency and to explicitly verify the parsed type (the stated intent of this test), add an isinstance check alongside the assertion, mirroring the pattern used for port.

💡 Suggested Change

Before:

    assert cfg.debug is True

After:

    assert isinstance(cfg.debug, bool)
    assert cfg.debug is True

Generated by cloud-assistant via Open Code Review.

@Memtensor-AI

Copy link
Copy Markdown
Collaborator

✅ Automated Test Results: PASSED

All tests passed (3/3 executed). memos_python_core/changed-repo-python: 3/3. Duration: 9s [advisory, non-gating] AI-generated tests on branch test/auto-gen-af001f99020e3d90-20260928220654: 120/120 passed — these do NOT affect the PR verdict; review the branch manually.

Branch: sq/env-config-optional

@Memtensor-AI Memtensor-AI added status:ready Ready for implementation; waiting for assignee or AI dispatch | 可进入实现,等待认领或派发 and removed status:in-progress Someone or AI is working on it | 人工或 AI 正在处理 labels Sep 28, 2026
@github-actions

Copy link
Copy Markdown
Contributor

This PR has been automatically marked as stale due to inactivity.

@github-actions github-actions Bot added the status:stale Auto-marked stale after 30d inactivity; closes 7d later unless exempt. label Oct 30, 2026
@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

This PR has been automatically closed due to inactivity.

@github-actions github-actions Bot closed this Nov 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:scheduler 调度模块 status:ready Ready for implementation; waiting for assignee or AI dispatch | 可进入实现,等待认领或派发 status:stale Auto-marked stale after 30d inactivity; closes 7d later unless exempt.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EnvConfigMixin.from_env raises an empty ValueError for absent optional fields

4 participants