mobyw / nonebot-plugin-txt2img

适用于 Nonebot2 的轻量文字转图片插件
MIT License
24 stars 4 forks source link

关于pydantic.errors.PydanticUserError: A non-annotated attribute was detected #10

Open lbsucceed opened 1 month ago

lbsucceed commented 1 month ago

在使用作者代码时出现此报错

pydantic.errors.PydanticUserError: A non-annotated attribute was detected: type = 'image'. All model fields require a type annotation; if type is not meant to be a field, you may be able to resolve this error by annotating it as a ClassVar or updating model_config['ignored_types'].

For further information visit https://errors.pydantic.dev/2.8/u/model-field-missing-annotation

在pydantic2.8版本中Pydantic 要求所有模型字段都有类型注释。如果 type 是非字段属性,则应将其注释为 ClassVar。 作者的pydantic 版本应该是先前的,需要手动将model中的两个类修改成如下:

class ImageBackground(BaseModel):
    """
    图片填充背景
    """

    type: ClassVar[str]  = "image"
    """
    背景类型:图片
    """
    image: Path
    """
    图片文件路径
    """

class ColorBackground(BaseModel):
    """
    纯色背景
    """

    type: ClassVar[str] = "color"
    """
    背景类型:纯色
    """
    color: Color
    """
    背景颜色
    """
mobyw commented 1 month ago

插件目前只支持 pydantic v1,近期会更新支持 v2