🐛 修复当消息与不支持的类型相加时抛出的异常类型错误 (#1166)

This commit is contained in:
Mix 2022-08-22 14:39:00 +08:00 committed by GitHub
parent be5ac88a18
commit 92ff1df419
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 6 deletions

View File

@ -186,7 +186,7 @@ class Message(List[TMS], abc.ABC):
elif isinstance(other, Iterable): elif isinstance(other, Iterable):
self.extend(other) self.extend(other)
else: else:
raise ValueError(f"Unsupported type: {type(other)}") # pragma: no cover raise TypeError(f"Unsupported type {type(other)!r}")
return self return self
@overload @overload

View File

@ -11,11 +11,11 @@ def test_template_basis():
def test_template_message(): def test_template_message():
Message = make_fake_message() Message = make_fake_message()
template = Message.template("{a:custom}{b:text}{c:image}") template = Message.template("{a:custom}{b:text}{c:image}/{d}")
@template.add_format_spec @template.add_format_spec
def custom(input: str) -> str: def custom(input: str) -> str:
return input + "-custom!" return f"{input}-custom!"
try: try:
template.add_format_spec(custom) template.add_format_spec(custom)
@ -24,12 +24,17 @@ def test_template_message():
else: else:
raise AssertionError("Should raise ValueError") raise AssertionError("Should raise ValueError")
format_args = {"a": "custom", "b": "text", "c": "https://example.com/test"} format_args = {
"a": "custom",
"b": "text",
"c": "https://example.com/test",
"d": 114,
}
formatted = template.format(**format_args) formatted = template.format(**format_args)
assert template.format_map(format_args) == formatted assert template.format_map(format_args) == formatted
assert formatted.extract_plain_text() == "custom-custom!text" assert formatted.extract_plain_text() == "custom-custom!text/114"
assert str(formatted) == "custom-custom!text[fake:image]" assert str(formatted) == "custom-custom!text[fake:image]/114"
def test_rich_template_message(): def test_rich_template_message():