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: event.dict() 方法调用时出现异常 KeyError: 'type' #473

Closed
Ailitonia opened this issue Aug 6, 2021 · 1 comment
Closed

Bug: event.dict() 方法调用时出现异常 KeyError: 'type' #473

Ailitonia opened this issue Aug 6, 2021 · 1 comment
Labels
bug Something isn't working

Comments

@Ailitonia
Copy link
Contributor

描述问题:

使用 event.dict() 方法报错

如何复现?

编写测试插件

from nonebot.typing import T_State
from nonebot.plugin import on_message
from nonebot.matcher import Matcher
from nonebot.adapters.cqhttp.event import Event
from nonebot.adapters.cqhttp.bot import Bot
from nonebot.message import run_preprocessor


test = on_message()


@run_preprocessor
async def handle_run_preprocessor(matcher: Matcher, bot: Bot, event: Event, state: T_State):
    print(event.dict())

期望的结果

正常输出

环境信息:

  • OS: Windows 10 21H1
  • Python Version: 3.9.6
  • Nonebot Version: 2.0.0a14

协议端信息:

  • 协议端: go-cqhttp
  • 协议端版本: 1.0.0-beta5

截图或日志

Traceback (most recent call last):
  File "C:\Users\ailitonia\PycharmProjects\nonebot2_miya\venv\lib\site-packages\nonebot\message.py", line 126, in _check_matcher
    await _run_matcher(Matcher, bot, event, state)
> File "C:\Users\ailitonia\PycharmProjects\nonebot2_miya\venv\lib\site-packages\nonebot\message.py", line 139, in _run_matcher
    await asyncio.gather(*coros)
  File "C:\Users\ailitonia\PycharmProjects\nonebot2_miya\omega_miya\utils\test.py", line 24, in handle_run_preprocessor
    print('event dict', event.dict())
  File "pydantic\main.py", line 510, in pydantic.main.BaseModel.dict
    return dict(
  File "pydantic\main.py", line 875, in _iter
    v = self._get_value(
  File "pydantic\main.py", line 816, in pydantic.main.BaseModel._get_value
    return v.__class__(*seq_args) if is_namedtuple(v.__class__) else v.__class__(seq_args)
  File "C:\Users\ailitonia\PycharmProjects\nonebot2_miya\venv\lib\site-packages\nonebot\adapters\_base.py", line 330, in __init__
    self.extend(self._construct(message))
  File "C:\Users\ailitonia\PycharmProjects\nonebot2_miya\venv\lib\site-packages\nonebot\adapters\_base.py", line 400, in extend
    for segment in obj:
  File "C:\Users\ailitonia\PycharmProjects\nonebot2_miya\venv\lib\site-packages\nonebot\adapters\cqhttp\message.py", line 265, in _construct
    yield MessageSegment(seg["type"], seg.get("data") or {})
  File "C:\Users\ailitonia\PycharmProjects\nonebot2_miya\venv\lib\site-packages\nonebot\adapters\_base.py", line 278, in __getitem__
    return self.data[key]
KeyError: 'type'

其他

def __getitem__(self, key):
return getattr(self, key)

def __getitem__(self, key: str):
return self.data[key]

cqhttp-adapter 似乎没有根据 MessageSegment 基类更新自己的解析方法

(或者 MessageSegment 基类魔法方法更新也需要考虑 adapter 兼容?)

@Ailitonia Ailitonia added the bug Something isn't working label Aug 6, 2021
@mnixry
Copy link
Member

mnixry commented Aug 6, 2021

已经在35642288e97a84 中修复, 将会在下个版本发布

@mnixry mnixry closed this as completed Aug 6, 2021
@mnixry mnixry changed the title Bug: event.dict() 方法异常 Bug: event.dict() 方法调用时出现异常 KeyError: 'type' Aug 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

No branches or pull requests

2 participants