From 65374685cca538cb8536e698cef0376c182f24b2 Mon Sep 17 00:00:00 2001 From: suzmii Date: Thu, 19 Feb 2026 23:27:50 +0800 Subject: [PATCH] =?UTF-8?q?fix(config):=20=E5=A4=84=E7=90=86=20=5Fconvert?= =?UTF-8?q?=5Ffield=20=E5=87=BD=E6=95=B0=E4=B8=AD=E8=8B=A5=E5=B9=B2?= =?UTF-8?q?=E8=BE=B9=E7=95=8C=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 支持裸 list/set/tuple 注解(无类型参数)时的安全转换 2. 修复 tuple[T, ...] 不定长元组解析逻辑 3. 对非法 tuple 注解(如 tuple[str, int, ...])给出明确错误提示 4. 字符串"false"转换bool类型时不再转为True --- src/config/config_base.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/config/config_base.py b/src/config/config_base.py index 1baa9372..9b131dae 100644 --- a/src/config/config_base.py +++ b/src/config/config_base.py @@ -77,6 +77,8 @@ class ConfigBase: # 检查提供的value是否为list if not isinstance(value, list): raise TypeError(f"Expected an list for {field_type.__name__}, got {type(value).__name__}") + if field_type_args == () and field_origin_type: + return field_origin_type(value) if field_origin_type is list: # 如果列表元素类型是ConfigBase的子类,则对每个元素调用from_dict @@ -90,8 +92,18 @@ class ConfigBase: elif field_origin_type is set: return {cls._convert_field(item, field_type_args[0]) for item in value} elif field_origin_type is tuple: + # fix: support Tuple[int, ...] + if len(field_type_args) == 2 and field_type_args[1] is Ellipsis: + return tuple(cls._convert_field(item, field_type_args[0]) for item in value) + + elif Ellipsis in field_type_args: + raise TypeError( + f"Invalid tuple annotation: {field_type}. " + "Only tuple[T, ...] (variadic homogeneous) or tuple[T1, T2, ...] (fixed length) is supported." + ) + # 检查提供的value长度是否与类型参数一致 - if len(value) != len(field_type_args): + elif len(value) != len(field_type_args): raise TypeError( f"Expected {len(field_type_args)} items for {field_type.__name__}, got {len(value)}" ) @@ -157,6 +169,10 @@ class ConfigBase: if field_type is Any or isinstance(value, field_type): return value + + # fix: bool("false") => True + if field_type is bool and type(value) is str and value.lower() in ("f", "false", "0"): + return False # 其他类型,尝试直接转换 try: