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

Co-authored-by: Ju4tCode <42488585+yanyongyu@users.noreply.github.com>
This commit is contained in:
student_2333 2024-02-12 17:53:50 +08:00 committed by GitHub
parent ef7782167f
commit 11142253fb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 6 additions and 1 deletions

View File

@ -285,7 +285,7 @@ class Message(List[TMS], abc.ABC):
消息内是否存在给定消息段或给定类型的消息段
"""
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:

View File

@ -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(