Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] matcher插件部分logger.info(event)报错 #31

Closed
xingdeshoumin opened this issue Feb 26, 2025 · 4 comments
Closed

[BUG] matcher插件部分logger.info(event)报错 #31

xingdeshoumin opened this issue Feb 26, 2025 · 4 comments
Labels
bug Something isn't working

Comments

@xingdeshoumin
Copy link
Contributor

Python 版本

3.11.2

SuggarChat 版本

1.3.1

发生了什么?

  1. 日志参数传递格式不匹配
    从代码片段看,logger.info()的方法签名是info(__message, *args, **kwargs),即第一个参数必须是字符串格式的日志模板,后续参数用于填充模板中的占位符。如果直接传入event对象,且未定义__message的格式化逻辑,可能导致类型不兼容或参数解析异常

如何复现

将README中demo放入自定义插件

期望的结果?

demo例程,规范调用logger

SuggarChat 运行日志

02-26 04:04:43 [SUCCESS] nonebot | OneBot V11 2240541753 | [notice.notify.poke]: {'time': 1740542683, 'self_id': 2240541753, 'post_type': 'notice', 'notice_type': 'notify', 'sub_type': 'poke', 'user_id': 2411426278, 'group_id': None, 'target_id': 2240541753, 'sender_id': 2411426278, 'action': '戳了戳', 'suffix': '', 'action_img_url': 'http://tianquan.gtimg.cn/nudgeaction/item/0/expression.jpg'}
02-26 04:04:43 [DEBUG] nonebot | Checking for matchers in priority 1...
02-26 04:04:43 [INFO] nonebot | Event will be handled by Matcher(type='notice', module=nonebot_plugin_suggarchat.suggar, lineno=324)
02-26 04:04:43 [DEBUG] nonebot | Running Matcher(type='notice', module=nonebot_plugin_suggarchat.suggar, lineno=324)
02-26 04:04:43 [DEBUG] nonebot | Running handler Dependent(call=_)
02-26 04:04:43 [INFO] nonebot | Event will be handled by Matcher(type='notice', module=nonebot_plugin_suggarchat.suggar, lineno=340)
02-26 04:04:43 [DEBUG] nonebot | Running Matcher(type='notice', module=nonebot_plugin_suggarchat.suggar, lineno=340)
02-26 04:04:43 [DEBUG] nonebot | Running handler Dependent(call=_)
02-26 04:04:43 [DEBUG] nonebot | Handler Dependent(call=_) skipped
02-26 04:04:43 [INFO] nonebot | Matcher(type='notice', module=nonebot_plugin_suggarchat.suggar, lineno=324) running complete
02-26 04:04:43 [DEBUG] nonebot | Handler Dependent(call=_) skipped
02-26 04:04:43 [INFO] nonebot | Matcher(type='notice', module=nonebot_plugin_suggarchat.suggar, lineno=340) running complete
02-26 04:04:43 [DEBUG] nonebot | Checking for matchers in priority 10...
02-26 04:04:43 [INFO] nonebot | Event will be handled by Matcher(type='notice', module=nonebot_plugin_suggarchat.suggar, lineno=337)
02-26 04:04:43 [DEBUG] nonebot | Running Matcher(type='notice', module=nonebot_plugin_suggarchat.suggar, lineno=337)
02-26 04:04:43 [DEBUG] nonebot | Running handler Dependent(call=_)
02-26 04:04:43 [INFO] nonebot_plugin_suggarchat | 开始为这个类型 poke 的事件运行处理。
02-26 04:04:43 [INFO] nonebot_plugin_suggarchat | 开始处理优先级为 10 的 poke 事件。
02-26 04:04:43 [INFO] nonebot_plugin_suggarchat | 开始运行处理器: '_'(~/app/data/kaqi_bot/kaqi_bot_nb/src/plugins/capoo/__init__.py:11)
02-26 04:04:43 [INFO] capoo | 收到聊天事件!
02-26 04:04:43 [ERROR] nonebot_plugin_suggarchat | 在运行处理器 '_'(~/app/data/kaqi_bot/kaqi_bot_nb/src/plugins/capoo/__init__.py:11) 时遇到了问题
02-26 04:04:43 [ERROR] nonebot_plugin_suggarchat | Exception type: AttributeError
02-26 04:04:43 [ERROR] nonebot_plugin_suggarchat | Exception message: 'PokeEvent' object has no attribute '_PokeEvent__nbevent'
02-26 04:04:43 [ERROR] nonebot_plugin_suggarchat |   File "/app/data/kaqi_bot/kaqi_env/lib/python3.11/site-packages/nonebot_plugin_suggarchat/matcher.py", line 139, in trigger_event
    await run_handle(handler, event,*new_args_tuple, **f_kwargs)
  File "/app/data/kaqi_bot/kaqi_env/lib/python3.11/site-packages/nonebot_plugin_suggarchat/matcher.py", line 20, in run_handle
    await handler(event, *args,**kwargs)
  File "/app/data/kaqi_bot/kaqi_bot_nb/src/plugins/capoo/__init__.py", line 13, in _
    logger.info(event)
  File "/app/data/kaqi_bot/kaqi_env/lib/python3.11/site-packages/loguru/_logger.py", line 2078, in info
    __self._log("INFO", False, __self._options, __message, args, kwargs)
  File "/app/data/kaqi_bot/kaqi_env/lib/python3.11/site-packages/loguru/_logger.py", line 2024, in _log
    "message": str(message),
               ^^^^^^^^^^^^
  File "/app/data/kaqi_bot/kaqi_env/lib/python3.11/site-packages/nonebot_plugin_suggarchat/event.py", line 314, in __str__
    return f"SUGGARPOKEEVENT({self.__event_type},{self.__nbevent},{self.__modelResponse},{self.__user_id},{self.__send_message})"
                                                  ^^^^^^^^^^^^^^

02-26 04:04:43 [INFO] nonebot_plugin_suggarchat | '_'(~/app/data/kaqi_bot/kaqi_bot_nb/src/plugins/capoo/__init__.py:11任务已结束。
02-26 04:04:43 [DEBUG] nonebot_plugin_suggarchat | Start to get response with model deepseek-ai/DeepSeek-V3
02-26 04:04:43 [DEBUG] nonebot_plugin_suggarchat | Preset:__main__
02-26 04:04:43 [DEBUG] nonebot_plugin_suggarchat | Key:eyJhbGc...
02-26 04:04:43 [DEBUG] nonebot_plugin_suggarchat | API base_url:https://api.studio.nebius.ai/v1/
02-26 04:04:46 [INFO] nonebot_plugin_suggarchat | 开始为这个类型 poke 的事件运行处理。
02-26 04:04:46 [INFO] nonebot_plugin_suggarchat | 开始处理优先级为 10 的 poke 事件。
02-26 04:04:46 [INFO] nonebot_plugin_suggarchat | 开始运行处理器: '_'(~/app/data/kaqi_bot/kaqi_bot_nb/src/plugins/capoo/__init__.py:11)
02-26 04:04:46 [INFO] capoo | 收到聊天事件!
02-26 04:04:46 [ERROR] nonebot_plugin_suggarchat | 在运行处理器 '_'(~/app/data/kaqi_bot/kaqi_bot_nb/src/plugins/capoo/__init__.py:11) 时遇到了问题
02-26 04:04:46 [ERROR] nonebot_plugin_suggarchat | Exception type: AttributeError
02-26 04:04:46 [ERROR] nonebot_plugin_suggarchat | Exception message: 'PokeEvent' object has no attribute '_PokeEvent__nbevent'
02-26 04:04:46 [ERROR] nonebot_plugin_suggarchat |   File "/app/data/kaqi_bot/kaqi_env/lib/python3.11/site-packages/nonebot_plugin_suggarchat/matcher.py", line 139, in trigger_event
    await run_handle(handler, event,*new_args_tuple, **f_kwargs)
  File "/app/data/kaqi_bot/kaqi_env/lib/python3.11/site-packages/nonebot_plugin_suggarchat/matcher.py", line 20, in run_handle
    await handler(event, *args,**kwargs)
  File "/app/data/kaqi_bot/kaqi_bot_nb/src/plugins/capoo/__init__.py", line 13, in _
    logger.info(event)
  File "/app/data/kaqi_bot/kaqi_env/lib/python3.11/site-packages/loguru/_logger.py", line 2078, in info
    __self._log("INFO", False, __self._options, __message, args, kwargs)
  File "/app/data/kaqi_bot/kaqi_env/lib/python3.11/site-packages/loguru/_logger.py", line 2024, in _log
    "message": str(message),
               ^^^^^^^^^^^^
  File "/app/data/kaqi_bot/kaqi_env/lib/python3.11/site-packages/nonebot_plugin_suggarchat/event.py", line 314, in __str__
    return f"SUGGARPOKEEVENT({self.__event_type},{self.__nbevent},{self.__modelResponse},{self.__user_id},{self.__send_message})"
                                                  ^^^^^^^^^^^^^^

02-26 04:04:46 [INFO] nonebot_plugin_suggarchat | '_'(~/app/data/kaqi_bot/kaqi_bot_nb/src/plugins/capoo/__init__.py:11任务已结束。
02-26 04:04:46 [DEBUG] nonebot | OneBot V11 | Calling API send_msg
@xingdeshoumin xingdeshoumin added the bug Something isn't working label Feb 26, 2025
@xingdeshoumin
Copy link
Contributor Author

额外测试

这个错误是由于 ChatEvent 对象缺少私有属性 __nbevent 导致的。以下是逐步解释和解决方案:

错误原因分析

  1. 属性缺失:错误信息 'ChatEvent' object has no attribute '_ChatEvent__nbevent' 表明尝试访问私有属性 __nbevent,但该属性未被正确初始化。
  2. 私有属性机制:Python 中双下划线开头的属性(如 __nbevent)会被名称修饰(name mangling),实际存储为 _ClassName__attribute(这里是 _ChatEvent__nbevent)。如果在类内部或构造函数未正确定义该属性,访问时会报错。
02-26 09:43:03 [ERROR] nonebot_plugin_suggarchat | 在运行处理器 '_'(~/app/data/kaqi_bot/kaqi_bot_nb/src/plugins/capoo/__init__.py:33) 时遇到了问题
02-26 09:43:03 [ERROR] nonebot_plugin_suggarchat | Exception type: AttributeError
02-26 09:43:03 [ERROR] nonebot_plugin_suggarchat | Exception message: 'ChatEvent' object has no attribute '_ChatEvent__nbevent'
02-26 09:43:03 [ERROR] nonebot_plugin_suggarchat |   File "/app/data/kaqi_bot/kaqi_env/lib/python3.11/site-packages/nonebot_plugin_suggarchat/matcher.py", line 139, in trigger_event
    await run_handle(handler, event,*new_args_tuple, **f_kwargs)
  File "/app/data/kaqi_bot/kaqi_env/lib/python3.11/site-packages/nonebot_plugin_suggarchat/matcher.py", line 20, in run_handle
    await handler(event, *args,**kwargs)
  File "/app/data/kaqi_bot/kaqi_bot_nb/src/plugins/capoo/__init__.py", line 35, in _
    logger.info(str(event))
                ^^^^^^^^^^
  File "/app/data/kaqi_bot/kaqi_env/lib/python3.11/site-packages/nonebot_plugin_suggarchat/event.py", line 260, in __str__
    return f"SUGGARCHATEVENT({self.__event_type},{self.__nbevent},{self.__modelResponse},{self.__user_id},{self.__send_message})"
                                                  ^^^^^^^^^^^^^^

@xingdeshoumin
Copy link
Contributor Author

猜测靠谱的原因

在Python类的继承中,若出现AttributeError: 'ChatEvent' object has no attribute '_ChatEvent__nbevent'错误,通常是由于子类访问父类私有属性时的命名规范冲突导致的。以下是结合用户代码的详细分析及解决方案:

一、问题定位

  1. ​私有属性的名称修饰(Name Mangling)​
    Python中双下划线开头的属性(如__nbevent)会被解释器自动添加类名前缀,形成_ClassName__attribute的格式。例如:

在父类SuggarEvent中,self.__nbevent实际存储为_SuggarEvent__nbevent。
在子类ChatEvent中,直接访问self.__nbevent会被解释为_ChatEvent__nbevent,而该属性并未定义。
2. ​继承中的属性访问
子类ChatEvent通过super().__init__调用了父类SuggarEvent的构造函数,父类初始化了_SuggarEvent__nbevent属性。但子类若直接访问self.__nbevent,实际上会寻找子类自身的_ChatEvent__nbevent属性,导致AttributeError。

@xingdeshoumin
Copy link
Contributor Author

xingdeshoumin commented Feb 26, 2025

报错解决

二、解决方案

  1. ​调整属性命名规范
    将父类中的私有属性__nbevent改为受保护属性​(单下划线开头),允许子类访问:

python

class SuggarEvent:
    def __init__(self, model_response: str, nbevent: BaseEvent, user_id: int, send_message: MessageSegment):
        self._nbevent = nbevent  # 改为单下划线(受保护属性)

class ChatEvent(SuggarEvent):
    def __str__(self):
        return f"SUGGARCHATEVENT({self.__event_type},{self._nbevent},...)"  # 子类直接访问

@JohnRichard4096
Copy link
Owner

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants