Repository navigation
Fix #2424: EnvConfigMixin.from_env raises an empty ValueError for absent optional fields - #2580
Open
syzsunshine219 wants to merge 2 commits into
Open
syzsunshine219 wants to merge 2 commits into
syzsunshine219 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
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
marked this pull request as ready for review
November 9, 2026 15:47
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Restores the change proposed in #2426 for review against
mainafter its former base branch,dev-v2.0.36, was deleted. The source commits and authors are preserved.Source head:
b66bbea6e17dd56540bc1d331071930aae2ee8b3This 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 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
@wustzdyplease review this PR.Reviewer Checklist