修复代码格式和文件名大小写问题
This commit is contained in:
@@ -135,16 +135,17 @@ class ConfigBase:
|
||||
"""返回配置类的字符串表示"""
|
||||
return f"{self.__class__.__name__}({', '.join(f'{f.name}={getattr(self, f.name)}' for f in fields(self))})"
|
||||
|
||||
|
||||
class ValidatedConfigBase(BaseModel):
|
||||
"""带验证的配置基类,继承自Pydantic BaseModel"""
|
||||
|
||||
|
||||
model_config = {
|
||||
"extra": "allow", # 允许额外字段
|
||||
"validate_assignment": True, # 验证赋值
|
||||
"arbitrary_types_allowed": True, # 允许任意类型
|
||||
"strict": True, # 如果设为 True 会完全禁用类型转换
|
||||
}
|
||||
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, data: dict):
|
||||
"""兼容原有的from_dict方法,增强错误信息"""
|
||||
@@ -152,42 +153,42 @@ class ValidatedConfigBase(BaseModel):
|
||||
return cls.model_validate(data)
|
||||
except ValidationError as e:
|
||||
enhanced_message = cls._create_enhanced_error_message(e, data)
|
||||
|
||||
|
||||
raise ValueError(enhanced_message) from e
|
||||
|
||||
|
||||
@classmethod
|
||||
def _create_enhanced_error_message(cls, e: ValidationError, data: dict) -> str:
|
||||
"""创建增强的错误信息"""
|
||||
enhanced_messages = []
|
||||
|
||||
|
||||
for error in e.errors():
|
||||
error_type = error.get('type', '')
|
||||
field_path = error.get('loc', ())
|
||||
input_value = error.get('input')
|
||||
|
||||
error_type = error.get("type", "")
|
||||
field_path = error.get("loc", ())
|
||||
input_value = error.get("input")
|
||||
|
||||
# 构建字段路径字符串
|
||||
field_path_str = '.'.join(str(p) for p in field_path)
|
||||
|
||||
field_path_str = ".".join(str(p) for p in field_path)
|
||||
|
||||
# 处理字符串类型错误
|
||||
if error_type == 'string_type' and len(field_path) >= 2:
|
||||
if error_type == "string_type" and len(field_path) >= 2:
|
||||
parent_field = field_path[0]
|
||||
element_index = field_path[1]
|
||||
|
||||
|
||||
# 尝试获取父字段的类型信息
|
||||
parent_field_info = cls.model_fields.get(parent_field)
|
||||
|
||||
if parent_field_info and hasattr(parent_field_info, 'annotation'):
|
||||
|
||||
if parent_field_info and hasattr(parent_field_info, "annotation"):
|
||||
expected_type = parent_field_info.annotation
|
||||
|
||||
|
||||
# 获取实际的父字段值
|
||||
actual_parent_value = data.get(parent_field)
|
||||
|
||||
|
||||
# 检查是否是列表类型错误
|
||||
if get_origin(expected_type) is list and isinstance(actual_parent_value, list):
|
||||
list_element_type = get_args(expected_type)[0] if get_args(expected_type) else str
|
||||
actual_item_type = type(input_value).__name__
|
||||
expected_element_name = getattr(list_element_type, '__name__', str(list_element_type))
|
||||
|
||||
expected_element_name = getattr(list_element_type, "__name__", str(list_element_type))
|
||||
|
||||
enhanced_messages.append(
|
||||
f"字段 '{field_path_str}' 类型错误: "
|
||||
f"期待类型 List[{expected_element_name}],"
|
||||
@@ -203,31 +204,30 @@ class ValidatedConfigBase(BaseModel):
|
||||
else:
|
||||
# 回退到原始错误信息
|
||||
enhanced_messages.append(f"字段 '{field_path_str}': {error.get('msg', str(error))}")
|
||||
|
||||
|
||||
# 处理缺失字段错误
|
||||
elif error_type == 'missing':
|
||||
elif error_type == "missing":
|
||||
enhanced_messages.append(f"缺少必需字段: '{field_path_str}'")
|
||||
|
||||
|
||||
# 处理模型类型错误
|
||||
elif error_type in ['model_type', 'dict_type', 'is_instance_of']:
|
||||
field_name = field_path[0] if field_path else 'unknown'
|
||||
elif error_type in ["model_type", "dict_type", "is_instance_of"]:
|
||||
field_name = field_path[0] if field_path else "unknown"
|
||||
field_info = cls.model_fields.get(field_name)
|
||||
|
||||
if field_info and hasattr(field_info, 'annotation'):
|
||||
|
||||
if field_info and hasattr(field_info, "annotation"):
|
||||
expected_type = field_info.annotation
|
||||
expected_name = getattr(expected_type, '__name__', str(expected_type))
|
||||
expected_name = getattr(expected_type, "__name__", str(expected_type))
|
||||
actual_name = type(input_value).__name__
|
||||
|
||||
|
||||
enhanced_messages.append(
|
||||
f"字段 '{field_name}' 类型错误: "
|
||||
f"期待类型 {expected_name},实际类型 {actual_name} (值: {input_value})"
|
||||
)
|
||||
else:
|
||||
enhanced_messages.append(f"字段 '{field_path_str}': {error.get('msg', str(error))}")
|
||||
|
||||
|
||||
# 处理其他类型错误
|
||||
else:
|
||||
enhanced_messages.append(f"字段 '{field_path_str}': {error.get('msg', str(error))}")
|
||||
|
||||
return "配置验证失败:\n" + "\n".join(f" - {msg}" for msg in enhanced_messages)
|
||||
|
||||
return "配置验证失败:\n" + "\n".join(f" - {msg}" for msg in enhanced_messages)
|
||||
|
||||
Reference in New Issue
Block a user