Repository navigation
fix: tolerate invalid env vars in settings, with SWANLAB_STRICT_ENV toggle - #1720
Closed
SAKURA-CAT wants to merge 3 commits into
Closed
SAKURA-CAT wants to merge 3 commits into
SAKURA-CAT wants to merge 3 commits into
Conversation
…-type (e.g. SWANLAB_RUN=1)
Member
Author
|
写的有点问题,算了,重写一个:我们收束一下设计,import swanlab时别报错就行,静默跳过不安全 |
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.
Summary
修复当环境变量直接映射到某个字段(嵌套模型或简单字段)但值无法解析时,
pydantic-settings抛出SettingsError、导致整个import swanlab崩溃的问题。同时提供SWANLAB_STRICT_ENV开关,便于调试时切换为严格模式。背景
Settings中的run/project/experiment/terminal/integration/core/probe均为嵌套子模型(complex 字段)。当环境变量直接命中字段名本身时,pydantic-settings会尝试json.loads()解析整个子模型,非 JSON 值即崩溃。简单字段(如bool)的非法值则在模型构造阶段抛出ValidationError。Changes
swanlab/sdk/internal/settings/__init__.pySTRICT_ENV(读取SWANLAB_STRICT_ENV,与SECRETS_DIR/CONFIG_DIR风格一致)_TolerantEnvSettingsSource(EnvSettingsSource):TypeAdapter预校验类型,非法值跳过并告警;strict 模式交给模型校验(保留before-validator行为)settings_customise_sources中用该子类替换默认的env_settingstests/unit/sdk/internal/settings/test_settings.pymode=cloud历史别名兼容TestStrictEnv:strict 模式下复杂/简单字段抛出、mode=cloud仍正常行为对照
SWANLAB_STRICT_ENV=1)SWANLAB_RUN=hello(复杂字段非法 JSON)SWANLAB_INTERACTIVE=notabool(简单字段非法值)SWANLAB_RUN={"id":"x"}(合法 JSON)SWANLAB_RUN_ID=xxx(嵌套子字段)SWANLAB_MODE=cloud(历史别名)onlineonline范围说明
.env文件、init 参数(代码传参)的错误始终抛出STRICT_ENV为模块级常量,import 后不变(与SECRETS_DIR/CONFIG_DIR一致);测试通过monkeypatch.setattr控制mode=cloud在 lenient 模式下被TypeAdapter拒绝(不在 Literal 中),但回退到默认值online,与 before-validator 转换结果一致Testing
uv run pytest tests/unit/sdk/internal/settings/→ 78 passeduv run ruff check/uv run basedpyright→ 通过uv run pytest tests/unit/sdk/internal tests/unit/sdk/cmd/init/test_init_e2e.py→ 1335 passed(需SWANLAB_RUN_ID未设置)Notes
tests/unit/conftest.py的 autouse fixture 未清理SWANLAB_RUN_ID等环境变量,属于既有的测试隔离缺陷,与本 PR 无关,后续可单独修复