diff --git a/src/common/toml_utils.py b/src/common/toml_utils.py index c5a0824a..0c34f6ab 100644 --- a/src/common/toml_utils.py +++ b/src/common/toml_utils.py @@ -25,14 +25,11 @@ def _format_toml_value(obj: Any, threshold: int, depth: int = 0) -> Any: # 处理列表类型 (list 或 Array) if isinstance(obj, (list, Array)): - # 如果包含字典/表,视为 AoT 的列表形式或内联表数组,保持结构递归处理 - if obj and isinstance(obj[0], (dict, Table)): + # 如果是纯 list (非 tomlkit Array) 且包含字典/表,视为 AoT 的列表形式 + # 保持结构递归处理,避免转换为 Inline Table Array (因为 Inline Table 必须单行,复杂对象不友好) + if isinstance(obj, list) and not isinstance(obj, Array) and obj and isinstance(obj[0], (dict, Table)): for i, item in enumerate(obj): - # 原地修改或递归处理 - if isinstance(obj, list): - obj[i] = _format_toml_value(item, threshold, depth) - else: - _format_toml_value(item, threshold, depth) + obj[i] = _format_toml_value(item, threshold, depth) return obj # 决定是否多行:仅在顶层且长度超过阈值时 @@ -67,4 +64,4 @@ def save_toml_with_format(data: Any, file_path: str, multiline_threshold: int = def format_toml_string(data: Any, multiline_threshold: int = 1) -> str: """格式化 TOML 数据并返回字符串""" formatted = _format_toml_value(data, multiline_threshold) if multiline_threshold >= 0 else data - return tomlkit.dumps(formatted) + return tomlkit.dumps(formatted) \ No newline at end of file