From 8f4daf6a1ee88ca3d1b3c27e6cd580682f99468e Mon Sep 17 00:00:00 2001 From: MemOS AutoDev Date: Mon, 28 Sep 2026 22:16:00 +0800 Subject: [PATCH 1/2] fix(mem_scheduler): EnvConfigMixin.from_env handles optional None defaults `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: #2424 --- .../mem_scheduler/general_modules/misc.py | 29 ++++-- tests/mem_scheduler/test_config.py | 95 ++++++++++++++++++- 2 files changed, 114 insertions(+), 10 deletions(-) diff --git a/src/memos/mem_scheduler/general_modules/misc.py b/src/memos/mem_scheduler/general_modules/misc.py index 078f5789b..31b78e06d 100644 --- a/src/memos/mem_scheduler/general_modules/misc.py +++ b/src/memos/mem_scheduler/general_modules/misc.py @@ -1,10 +1,11 @@ import json import os +import types from contextlib import suppress from datetime import datetime from queue import Empty, Full, Queue -from typing import TYPE_CHECKING, Any, Generic, TypeVar +from typing import TYPE_CHECKING, Any, Generic, TypeVar, Union, get_args, get_origin from dotenv import load_dotenv from pydantic import field_serializer @@ -65,21 +66,31 @@ def from_env(cls: type[T]) -> T: env_var = f"{prefix}{field_name.upper()}" field_type = field_info.annotation - if field_info.is_required() and env_var not in os.environ: - raise ValueError(f"Required environment variable {env_var} is missing") - if env_var in os.environ: raw_value = os.environ[env_var] field_values[field_name] = cls._parse_env_value(raw_value, field_type) - elif field_info.default is not None: - field_values[field_name] = field_info.default - else: - raise ValueError() + elif field_info.is_required(): + raise ValueError(f"Required environment variable {env_var} is missing") + # Non-required fields with no env var set: fall through and let + # pydantic apply the declared default (or default_factory) at + # construction time. Explicitly passing field_info.default here + # would leak PydanticUndefined for default_factory fields. return cls(**field_values) @classmethod def _parse_env_value(cls, value: str, target_type: type) -> Any: - """Converts environment variable string to appropriate type.""" + """Converts environment variable string to appropriate type. + + Unwraps ``Optional[T]`` / ``Union[T, None]`` / ``T | None`` to the + underlying ``T`` before dispatching, so optional numeric/bool fields + parse correctly instead of remaining as raw strings. + """ + origin = get_origin(target_type) + if origin is Union or origin is types.UnionType: + non_none_args = [arg for arg in get_args(target_type) if arg is not type(None)] + if len(non_none_args) == 1: + target_type = non_none_args[0] + if target_type is bool: return value.lower() in ("true", "1", "t", "y", "yes") if target_type is int: diff --git a/tests/mem_scheduler/test_config.py b/tests/mem_scheduler/test_config.py index f6d2ac66e..4b50b879c 100644 --- a/tests/mem_scheduler/test_config.py +++ b/tests/mem_scheduler/test_config.py @@ -4,9 +4,13 @@ from pathlib import Path from tempfile import NamedTemporaryFile, TemporaryDirectory +from typing import Optional +from pydantic import Field + +from memos.configs.base import BaseConfig from memos.configs.mem_scheduler import AuthConfig, GraphDBAuthConfig, OpenAIConfig, RabbitMQConfig -from memos.mem_scheduler.general_modules.misc import EnvConfigMixin +from memos.mem_scheduler.general_modules.misc import DictConversionMixin, EnvConfigMixin from memos.mem_scheduler.utils.config_utils import convert_config_to_env, flatten_dict @@ -93,6 +97,95 @@ def test_parse_env_value(self): # Test string parsing self.assertEqual(EnvConfigMixin._parse_env_value("test", str), "test") + def test_from_env_optional_none_field_uses_default(self): + """Regression for #2424: absent optional field with default=None must + NOT raise; it should fall back to the declared default (None here).""" + + class _OptionalNoneConfig(BaseConfig, DictConversionMixin, EnvConfigMixin): + host: str = Field(default="localhost") + port: int | None = Field(default=None) + + # Ensure the two env vars are absent, so we exercise the fallback path. + prefix = _OptionalNoneConfig.get_env_prefix() + saved = {} + for suffix in ("HOST", "PORT"): + key = f"{prefix}{suffix}" + if key in os.environ: + saved[key] = os.environ.pop(key) + try: + config = _OptionalNoneConfig.from_env() + self.assertEqual(config.host, "localhost") + self.assertIsNone(config.port) + finally: + for key, value in saved.items(): + os.environ[key] = value + + def test_from_env_optional_int_env_var_is_parsed_as_int(self): + """Regression for #2424 secondary: Optional[int] env var must parse as int.""" + + class _OptionalIntConfig(BaseConfig, DictConversionMixin, EnvConfigMixin): + port: int | None = Field(default=None) + + prefix = _OptionalIntConfig.get_env_prefix() + key = f"{prefix}PORT" + original = os.environ.get(key) + try: + os.environ[key] = "5672" + config = _OptionalIntConfig.from_env() + self.assertIsInstance(config.port, int) + self.assertEqual(config.port, 5672) + finally: + if original is None: + os.environ.pop(key, None) + else: + os.environ[key] = original + + def test_from_env_required_field_missing_raises_descriptive_error(self): + """Required-field path must still raise with a descriptive message + (not the previous bare ValueError()).""" + + class _RequiredFieldConfig(BaseConfig, DictConversionMixin, EnvConfigMixin): + api_key: str = Field(...) # required, no default + + prefix = _RequiredFieldConfig.get_env_prefix() + key = f"{prefix}API_KEY" + original = os.environ.pop(key, None) + try: + with self.assertRaises(ValueError) as ctx: + _RequiredFieldConfig.from_env() + self.assertIn(key, str(ctx.exception)) + finally: + if original is not None: + os.environ[key] = original + + def test_from_env_default_factory_field_uses_factory(self): + """Non-required field with a default_factory must use the factory + rather than raising when env var is absent.""" + + class _FactoryConfig(BaseConfig, DictConversionMixin, EnvConfigMixin): + tags: list[str] = Field(default_factory=list) + + prefix = _FactoryConfig.get_env_prefix() + key = f"{prefix}TAGS" + original = os.environ.pop(key, None) + try: + config = _FactoryConfig.from_env() + self.assertEqual(config.tags, []) + finally: + if original is not None: + os.environ[key] = original + + def test_parse_env_value_unwraps_optional(self): + """_parse_env_value must unwrap Optional[T] before dispatching.""" + # Deliberately exercise both the legacy typing.Optional[...] form and the + # PEP 604 X | None form, so the noqa below is intentional. + self.assertEqual(EnvConfigMixin._parse_env_value("42", Optional[int]), 42) # noqa: UP045 + self.assertEqual(EnvConfigMixin._parse_env_value("3.14", Optional[float]), 3.14) # noqa: UP045 + self.assertTrue(EnvConfigMixin._parse_env_value("true", Optional[bool])) # noqa: UP045 + self.assertEqual(EnvConfigMixin._parse_env_value("hello", Optional[str]), "hello") # noqa: UP045 + # PEP 604 union syntax + self.assertEqual(EnvConfigMixin._parse_env_value("7", int | None), 7) + def test_env_config_mixin_integration(self): """Test EnvConfigMixin integration with actual configuration classes""" # Set complete test environment variables From b66bbea6e17dd56540bc1d331071930aae2ee8b3 Mon Sep 17 00:00:00 2001 From: autodev-bot Date: Mon, 28 Sep 2026 22:31:06 +0800 Subject: [PATCH 2/2] test(mem_scheduler): assert Optional[bool] parse returns True not truthy 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. --- tests/mem_scheduler/test_config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/mem_scheduler/test_config.py b/tests/mem_scheduler/test_config.py index 4b50b879c..31f9d8f48 100644 --- a/tests/mem_scheduler/test_config.py +++ b/tests/mem_scheduler/test_config.py @@ -181,7 +181,7 @@ def test_parse_env_value_unwraps_optional(self): # PEP 604 X | None form, so the noqa below is intentional. self.assertEqual(EnvConfigMixin._parse_env_value("42", Optional[int]), 42) # noqa: UP045 self.assertEqual(EnvConfigMixin._parse_env_value("3.14", Optional[float]), 3.14) # noqa: UP045 - self.assertTrue(EnvConfigMixin._parse_env_value("true", Optional[bool])) # noqa: UP045 + self.assertIs(EnvConfigMixin._parse_env_value("true", Optional[bool]), True) # noqa: UP045 self.assertEqual(EnvConfigMixin._parse_env_value("hello", Optional[str]), "hello") # noqa: UP045 # PEP 604 union syntax self.assertEqual(EnvConfigMixin._parse_env_value("7", int | None), 7)