From fe75dad3bb21f4bcbff05d306749b0b22301a20e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A2=A8=E6=A2=93=E6=9F=92?= <1787882683@qq.com> Date: Wed, 24 Dec 2025 21:59:54 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=AF=B9Optiona?= =?UTF-8?q?l=E7=B1=BB=E5=9E=8B=E7=9A=84=E5=AE=89=E5=85=A8=E6=A3=80?= =?UTF-8?q?=E6=9F=A5=EF=BC=8C=E7=A6=81=E6=AD=A2=E5=A4=9A=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?Union?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/config/config_base.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/config/config_base.py b/src/config/config_base.py index 715ee944..1baa9372 100644 --- a/src/config/config_base.py +++ b/src/config/config_base.py @@ -120,6 +120,15 @@ class ConfigBase: if is_union_type: union_args = field_type_args if field_type_args else get_args(field_type) + + # 安全检查:只允许 T | None 形式的 Optional 类型,禁止 float | str 这种多类型 Union + non_none_types = [arg for arg in union_args if arg is not type(None)] + if len(non_none_types) > 1: + raise TypeError( + f"配置字段不支持多类型 Union(如 float | str),只支持 Optional 类型(如 float | None)。" + f"当前类型: {field_type}" + ) + # 如果值是 None 且 None 在 Union 中,直接返回 if value is None and type(None) in union_args: return None