Skip to content

Commit

Permalink
🐛 Fix: Message.__contains__() 未考虑 bool(MessageSegment) 存在 False 情…
Browse files Browse the repository at this point in the history
…况导致的异常结果 (#2572)

Co-authored-by: Ju4tCode <42488585+yanyongyu@users.noreply.github.com>
  • Loading branch information
lgc2333 and yanyongyu authored Feb 12, 2024
1 parent ef77821 commit 1114225
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion nonebot/internal/adapter/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ def __contains__(self, value: Union[TMS, str]) -> bool:
消息内是否存在给定消息段或给定类型的消息段
"""
if isinstance(value, str):
return bool(next((seg for seg in self if seg.type == value), None))
return next((seg for seg in self if seg.type == value), None) is not None
return super().__contains__(value)

def has(self, value: Union[TMS, str]) -> bool:
Expand Down
5 changes: 5 additions & 0 deletions tests/test_adapters/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ def test_message_contains():
assert message.has("foo") is False
assert "foo" not in message

assert not bool(FakeMessageSegment.text(""))
msg_with_empty_seg = FakeMessage([FakeMessageSegment.text("")])
assert msg_with_empty_seg.has("text") is True
assert "text" in msg_with_empty_seg


def test_message_only():
message = FakeMessage(
Expand Down

0 comments on commit 1114225

Please sign in to comment.