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

Fix: Message.__contains__() 未考虑 bool(MessageSegment) 存在 False 情况导致的异常结果 #2572

Merged
merged 6 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 4 additions & 0 deletions tests/test_adapters/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ def test_message_contains():
assert message.has("foo") is False
assert "foo" not in message

msg_with_empty_seg = FakeMessage([FakeMessageSegment.text("")])
yanyongyu marked this conversation as resolved.
Show resolved Hide resolved
assert msg_with_empty_seg.has("text") is True
assert "text" in msg_with_empty_seg
lgc2333 marked this conversation as resolved.
Show resolved Hide resolved


def test_message_only():
message = FakeMessage(
Expand Down
Loading