Skip to content

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

Open
syzsunshine219 wants to merge 2 commits into
MemTensor:mainfrom
Memtensor-AI:bugfix/autodev-2424-20260928140127315
Open

syzsunshine219 wants to merge 2 commits into
MemTensor:mainfrom
Memtensor-AI:bugfix/autodev-2424-20260928140127315

Conversation

@syzsunshine219

@syzsunshine219 syzsunshine219 commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

Restores the change proposed in #2426 for review against main after its former base branch, dev-v2.0.36, was deleted. The source commits and authors are preserved.

Source head: b66bbea6e17dd56540bc1d331071930aae2ee8b3

This PR is open for review. Review and CI against the current base are pending; validation claims in the original description below refer to the earlier snapshot. The original discussion and evidence remain in #2426.


Original change description

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

MemOS AutoDev and others added 2 commits September 28, 2026 22:16
…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
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.
@syzsunshine219
syzsunshine219 marked this pull request as ready for review November 9, 2026 15:47
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.

EnvConfigMixin.from_env raises an empty ValueError for absent optional fields

1 participant