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: 修复 MessageSegment 在有额外数据时报错 #1055

Merged
merged 4 commits into from
Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion nonebot/internal/adapter/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ def _validate(cls, value):
return value
if not isinstance(value, dict):
raise ValueError(f"Expected dict for MessageSegment, got {type(value)}")
return cls(**value)
if "type" not in value:
raise ValueError(
f"Expected dict with 'type' for MessageSegment, got {value}"
)
return cls(type=value["type"], data=value.get("data", {}))

def get(self, key: str, default: Any = None):
return asdict(self).get(key, default)
Expand Down
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ all = ["quart", "aiohttp", "httpx", "websockets"]
[tool.pytest.ini_options]
asyncio_mode = "auto"
addopts = "--cov=nonebot --cov-report=term-missing"
filterwarnings = [
"error",
"ignore::DeprecationWarning",
]

[tool.black]
line-length = 88
Expand Down
22 changes: 9 additions & 13 deletions tests/test_adapters/test_message.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pytest
from pydantic import ValidationError, parse_obj_as

from utils import make_fake_message
Expand Down Expand Up @@ -29,14 +30,15 @@ def test_segment_validate():
MessageSegment = Message.get_segment_class()

assert parse_obj_as(
MessageSegment, {"type": "text", "data": {"text": "text"}}
MessageSegment,
{"type": "text", "data": {"text": "text"}, "extra": "should be ignored"},
) == MessageSegment.text("text")

try:
with pytest.raises(ValidationError):
parse_obj_as(MessageSegment, "some str")
assert False
except ValidationError:
assert True

with pytest.raises(ValidationError):
parse_obj_as(MessageSegment, {"data": {}})


def test_segment():
Expand Down Expand Up @@ -129,11 +131,8 @@ def test_message_validate():

assert parse_obj_as(Message, Message([])) == Message([])

try:
with pytest.raises(ValidationError):
parse_obj_as(Message, Message_([]))
assert False
except ValidationError:
assert True

assert parse_obj_as(Message, "text") == Message([MessageSegment.text("text")])

Expand All @@ -146,8 +145,5 @@ def test_message_validate():
[MessageSegment.text("text"), {"type": "text", "data": {"text": "text"}}],
) == Message([MessageSegment.text("text"), MessageSegment.text("text")])

try:
with pytest.raises(ValidationError):
parse_obj_as(Message, object())
assert False
except ValidationError:
assert True