From ca55aca750a36b0f1193267917ff0e728a0aed23 Mon Sep 17 00:00:00 2001 From: SengokuCola <1026294844@qq.com> Date: Sat, 24 May 2025 19:24:34 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E5=A4=84=E7=90=86=E7=89=B9=E6=AE=8A?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/config/config_base.py | 14 +++++++++++++- template/bot_config_template.toml | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/config/config_base.py b/src/config/config_base.py index 92f6cf9d4..6b1c5fb56 100644 --- a/src/config/config_base.py +++ b/src/config/config_base.py @@ -1,5 +1,5 @@ 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") @@ -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()} # 处理基础类型,例如 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): return value diff --git a/template/bot_config_template.toml b/template/bot_config_template.toml index f3822729e..ef6bbfa5c 100644 --- a/template/bot_config_template.toml +++ b/template/bot_config_template.toml @@ -1,5 +1,5 @@ [inner] -version = "2.3.0" +version = "2.4.0" #----以下是给开发人员阅读的,如果你只是部署了麦麦,不需要阅读---- #如果你想要修改配置文件,请在修改后将version的值进行变更