fix:处理特殊类型

This commit is contained in:
SengokuCola
2025-05-24 19:24:34 +08:00
parent 93152b0c8d
commit ca55aca750
2 changed files with 14 additions and 2 deletions

View File

@@ -1,5 +1,5 @@
from dataclasses import dataclass, fields, MISSING from dataclasses import dataclass, fields, MISSING
from typing import TypeVar, Type, Any, get_origin, get_args from typing import TypeVar, Type, Any, get_origin, get_args, Literal
T = TypeVar("T", bound="ConfigBase") T = TypeVar("T", bound="ConfigBase")
@@ -102,6 +102,18 @@ class ConfigBase:
return {cls._convert_field(k, key_type): cls._convert_field(v, value_type) for k, v in value.items()} return {cls._convert_field(k, key_type): cls._convert_field(v, value_type) for k, v in value.items()}
# 处理基础类型,例如 int, str 等 # 处理基础类型,例如 int, str 等
if field_origin_type is type(None) and value is None: #处理Optional类型
return None
# 处理Literal类型
if field_origin_type is Literal or get_origin(field_type) is Literal:
# 获取Literal的允许值
allowed_values = get_args(field_type)
if value in allowed_values:
return value
else:
raise TypeError(f"Value '{value}' is not in allowed values {allowed_values} for Literal type")
if field_type is Any or isinstance(value, field_type): if field_type is Any or isinstance(value, field_type):
return value return value

View File

@@ -1,5 +1,5 @@
[inner] [inner]
version = "2.3.0" version = "2.4.0"
#----以下是给开发人员阅读的,如果你只是部署了麦麦,不需要阅读---- #----以下是给开发人员阅读的,如果你只是部署了麦麦,不需要阅读----
#如果你想要修改配置文件请在修改后将version的值进行变更 #如果你想要修改配置文件请在修改后将version的值进行变更