Skip to content

Fix #2424: EnvConfigMixin.from_env raises an empty ValueError for absent optional fields - #2426

Closed
Memtensor-AI wants to merge 2 commits into
MemTensor:dev-v2.0.36from
Memtensor-AI:bugfix/autodev-2424-20260928140127315
Closed

Memtensor-AI wants to merge 2 commits into
MemTensor:dev-v2.0.36from
Memtensor-AI:bugfix/autodev-2424-20260928140127315

Conversation

@Memtensor-AI

Copy link
Copy Markdown
Collaborator

Description

Fixed EnvConfigMixin.from_env raising an empty ValueError for absent optional fields (issue #2424). The bug was in src/memos/mem_scheduler/general_modules/misc.py: the loop compared field_info.default is not None, which incorrectly treated a legitimate default=None (as on port: int | None = None) as "no default set" and fell through to raise ValueError(). Also fixed the secondary _parse_env_value bug where Optional[int] annotations kept env vars as raw strings because the type dispatch never unwrapped Union[T, None].

The fix reworks from_env so that required-and-missing fields raise a descriptive ValueError naming the env var, and non-required absent fields are simply omitted from the constructor kwargs — letting pydantic apply the declared default or default_factory uniformly for None, non-None, and factory-produced defaults. _parse_env_value now unwraps both legacy typing.Optional[T] and PEP 604 T | None before dispatching to the bool/int/float branches.

Added 5 regression tests in tests/mem_scheduler/test_config.py: absent optional-None field falls back to default, Optional[int] env var parses as int, required-field missing env var raises with a descriptive message, default_factory field uses the factory, and _parse_env_value unwrap works for both syntaxes. Verified the exact repro from the issue (RabbitMQConfig(EnvConfigMixin, BaseModel) with port: Optional[int] = None) now returns port=None instead of raising.

Test evidence: pytest tests/mem_scheduler/ → 71 passed, 1 skipped; ruff format and ruff check on both changed files → clean.

Related Issue (Required): Fixes #2424

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Refactor (does not change functionality, e.g. code style improvements, linting)
  • Documentation update

How Has This Been Tested?

Automated tests are pending.

  • Unit Test
  • Test Script Or Test Steps (please provide)
  • Pipeline Automated API Test (please provide)

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 created related documentation issue/PR in MemOS-Docs (if applicable)
  • I have linked the issue to this PR (if applicable)
  • I have mentioned the person who will review this PR

@wustzdy please review this PR.

Reviewer Checklist

…aults

`from_env` treated `field.default is None` as "no default set" and raised a
bare `ValueError()`, so any config with a plain `Optional[T] = None` field
(e.g. `port: int | None = None` on `RabbitMQConfig`) could not be built from
the environment when the corresponding env var was absent. This broke the
scheduler init path where `RabbitMQConfig.from_env()` runs unconditionally.

Rework the loop to:
- raise a descriptive `ValueError` only for required fields whose env var
  is missing (previously the message was empty);
- omit absent non-required fields from the constructor kwargs so pydantic
  applies the declared default or `default_factory` (works uniformly for
  `None`, non-`None`, and factory-produced defaults).

Also fix `_parse_env_value` so it unwraps `Optional[T]` / `Union[T, None]`
/ `T | None` before dispatching, letting `Optional[int]` env vars parse as
`int` instead of staying as raw strings.

Adds five regression tests covering: absent optional-None field falls back
to default, `Optional[int]` env var parses as int, required-field missing
env var still raises with a descriptive message, `default_factory` field
uses the factory, and `_parse_env_value` unwrap works for both legacy
`Optional[T]` and PEP 604 `T | None`.

Refs: MemTensor#2424
@Memtensor-AI Memtensor-AI added ai:generated Generated or modified by AI | 由 AI 生成或修改 area:scheduler 调度模块 status:in-progress Someone or AI is working on it | 人工或 AI 正在处理 labels Sep 28, 2026
@Memtensor-AI

Memtensor-AI commented Sep 28, 2026 •

Copy link
Copy Markdown
Collaborator Author

🤖 Open Code Review

Target: PR #2426
Task: 2ab67b9f19f05d28
Base: dev-v2.0.36
Head: bugfix/autodev-2424-20260928140127315
Head SHA: b66bbea6e17dd56540bc1d331071930aae2ee8b3

✅ OpenCodeReview: Review complete: 0 finding(s) across 2 selected item(s).

Generated by cloud-assistant via Open Code Review.

@Memtensor-AI

Copy link
Copy Markdown
Collaborator Author

🔧 Open Code Review requested Agent fix

Open Code Review found 1 issue(s). I have resumed the development Agent to fix them.

  • Task: 2ab67b9f19f05d28
  • Fix attempt: 1/2
  • Finding delta: 0 repeated / 1 new / 0 likely resolved

The Agent will push a new commit to this PR branch. OCR will recheck after the commit is pushed.

OCR review flagged that assertTrue would still pass if _parse_env_value
returned the raw string 'true'. Use assertIs(..., True) to catch a
regression where Optional[bool] unwrapping is skipped and the value
short-circuits back to the input string.
@Memtensor-AI

Copy link
Copy Markdown
Collaborator Author

✅ Automated Test Results: PASSED

All tests passed (27/27 executed, 1 skipped). memos_github_open_source/smoke: 1/1, memos_python_core/changed-repo-python: 26 passed, 1 skipped. Duration: 9s [advisory, non-gating] AI-generated tests on branch test/auto-gen-2ab67b9f19f05d28-20260928222432: 94/94 passed — these do NOT affect the PR verdict; review the branch manually.

Branch: bugfix/autodev-2424-20260928140127315

@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.

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

Labels

ai:generated Generated or modified by AI | 由 AI 生成或修改 area:scheduler 调度模块 status:in-progress Someone or AI is working on it | 人工或 AI 正在处理 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.

2 participants