Repository navigation
Fix #2424: EnvConfigMixin.from_env raises an empty ValueError for absent optional fields - #2426
Memtensor-AI wants to merge 2 commits into
Conversation
…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
🤖 Open Code ReviewTarget: PR #2426 ✅ OpenCodeReview: Review complete: 0 finding(s) across 2 selected item(s). Generated by cloud-assistant via Open Code Review. |
🔧 Open Code Review requested Agent fixOpen Code Review found 1 issue(s). I have resumed the development Agent to fix them.
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.
✅ Automated Test Results: PASSEDAll 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: |
|
This PR has been automatically marked as stale due to inactivity. |
|
This PR has been automatically closed due to inactivity. |
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 legitimatedefault=None(as onport: int | None = None) as "no default set" and fell through toraise ValueError(). Also fixed the secondary_parse_env_valuebug whereOptional[int]annotations kept env vars as raw strings because the type dispatch never unwrappedUnion[T, None].The fix reworks
from_envso that required-and-missing fields raise a descriptiveValueErrornaming the env var, and non-required absent fields are simply omitted from the constructor kwargs — letting pydantic apply the declared default ordefault_factoryuniformly forNone, non-None, and factory-produced defaults._parse_env_valuenow unwraps both legacytyping.Optional[T]and PEP 604T | Nonebefore 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_factoryfield uses the factory, and_parse_env_valueunwrap works for both syntaxes. Verified the exact repro from the issue (RabbitMQConfig(EnvConfigMixin, BaseModel)withport: Optional[int] = None) now returnsport=Noneinstead of raising.Test evidence:
pytest tests/mem_scheduler/→ 71 passed, 1 skipped;ruff formatandruff checkon both changed files → clean.Related Issue (Required): Fixes #2424
Type of change
Please delete options that are not relevant.
How Has This Been Tested?
Automated tests are pending.
Checklist
@wustzdy please review this PR.
Reviewer Checklist