diff --git a/src/memos/mem_scheduler/general_modules/misc.py b/src/memos/mem_scheduler/general_modules/misc.py index 078f5789b..bf85df140 100644 --- a/src/memos/mem_scheduler/general_modules/misc.py +++ b/src/memos/mem_scheduler/general_modules/misc.py @@ -4,7 +4,7 @@ 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, get_args from dotenv import load_dotenv from pydantic import field_serializer @@ -68,18 +68,24 @@ def from_env(cls: type[T]) -> T: if field_info.is_required() and env_var not in os.environ: raise ValueError(f"Required environment variable {env_var} is missing") + # Absent optional fields are simply omitted so that pydantic + # applies the model's own default (previously a field whose + # default was None crashed with a bare, message-less ValueError). 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() return cls(**field_values) @classmethod - def _parse_env_value(cls, value: str, target_type: type) -> Any: + def _parse_env_value(cls, value: str, target_type: Any) -> Any: """Converts environment variable string to appropriate type.""" + # Unwrap Optional[X] / X | None so an optional int or float still + # parses as its inner type instead of staying a string. + 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] 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_env_config_mixin.py b/tests/mem_scheduler/test_env_config_mixin.py new file mode 100644 index 000000000..4614a5e80 --- /dev/null +++ b/tests/mem_scheduler/test_env_config_mixin.py @@ -0,0 +1,52 @@ + +from pydantic import BaseModel + +from memos.mem_scheduler.general_modules.misc import EnvConfigMixin + + +class SampleConfig(EnvConfigMixin, BaseModel): + host: str = "localhost" + port: int | None = None + debug: bool = False + + +def test_from_env_missing_optional_field_uses_default(monkeypatch): + # a field whose default is None and whose env var is absent used to hit + # a bare `raise ValueError()` with no message + monkeypatch.delenv("MEMSCHEDULER_SAMPLE_PORT", raising=False) + monkeypatch.delenv("MEMSCHEDULER_SAMPLE_HOST", raising=False) + monkeypatch.delenv("MEMSCHEDULER_SAMPLE_DEBUG", raising=False) + + cfg = SampleConfig.from_env() + + assert cfg.host == "localhost" + assert cfg.port is None + assert cfg.debug is False + + +def test_from_env_parses_optional_and_bool_fields(monkeypatch): + # Optional[int] used to keep the raw string because only exact `int` + # annotations were parsed + monkeypatch.setenv("MEMSCHEDULER_SAMPLE_PORT", "5672") + monkeypatch.setenv("MEMSCHEDULER_SAMPLE_DEBUG", "true") + + cfg = SampleConfig.from_env() + + assert cfg.port == 5672 + assert isinstance(cfg.port, int) + assert cfg.debug is True + + +def test_from_env_still_requires_required_fields(monkeypatch): + from pydantic import Field + + class RequiredConfig(EnvConfigMixin, BaseModel): + api_key: str = Field() + + monkeypatch.delenv("MEMSCHEDULER_REQUIRED_API_KEY", raising=False) + try: + RequiredConfig.from_env() + except ValueError as e: + assert "MEMSCHEDULER_REQUIRED_API_KEY" in str(e) + else: + raise AssertionError("expected ValueError for missing required env var")