Skip to content

Commit 60a922a

Browse files
committed
⬆️ upgrade
1 parent cdabe15 commit 60a922a

File tree

7 files changed

+41
-15
lines changed

7 files changed

+41
-15
lines changed

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ authors = [
77
dependencies = [
88
"tarina<0.7,>=0.6.3",
99
"nepattern<1.0,>=0.7.4",
10-
"arclet-alconna<2.0,>=1.8.31",
10+
"arclet-alconna<2.0,>=1.8.33",
1111
"arclet-alconna-tools>=0.7.10",
1212
"importlib-metadata>=4.13.0",
1313
"nonebot2>=2.3.0",
@@ -112,3 +112,4 @@ format = { composite = ["isort ./src/ ./example/ ./tests/","black ./src/ ./examp
112112

113113
[tool.pytest.ini_options]
114114
asyncio_mode = "auto"
115+
asyncio_default_fixture_loop_scope = "session"

src/nonebot_plugin_alconna/uniseg/message.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from tarina import lang
1313
from tarina.lang.model import LangItem
1414
from tarina.context import ContextModel
15+
from nonebot.compat import custom_validation
1516
from nonebot.exception import FinishedException
1617
from nonebot.internal.adapter import Bot, Event, Message
1718
from nonebot.internal.matcher import current_bot, current_event
@@ -1473,6 +1474,7 @@ def load(cls: "type[UniMessage[Segment]]", data: Union[str, list[dict[str, Any]]
14731474
return cls(get_segment_class(seg_data["type"]).load(seg_data) for seg_data in _data)
14741475

14751476

1477+
@custom_validation
14761478
@dataclass
14771479
class Receipt:
14781480
bot: Bot
@@ -1608,3 +1610,13 @@ async def finish(
16081610
):
16091611
await self.send(message, fallback, at_sender, reply_to, delay, **kwargs)
16101612
raise FinishedException
1613+
1614+
@classmethod
1615+
def __get_validators__(cls):
1616+
yield cls._validate
1617+
1618+
@classmethod
1619+
def _validate(cls, value) -> Self:
1620+
if isinstance(value, cls):
1621+
return value
1622+
raise ValueError(f"Type {type(value)} can not be converted to {cls}")

src/nonebot_plugin_alconna/uniseg/utils/saa.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from typing import Any
33

44
from nonebot import require
5-
from nonebot.compat import model_dump
5+
from nonebot.compat import PYDANTIC_V2, ConfigDict, model_dump
66
from nonebot.internal.matcher import current_bot, current_event
77

88
from nonebot_plugin_alconna.uniseg.message import Text, AtAll, Receipt, UniMessage
@@ -55,8 +55,12 @@ class UnisegMessageId(MessageId):
5555
adapter_name: str
5656
message_id: Any
5757

58-
class Config:
59-
arbitrary_types_allowed = True
58+
if PYDANTIC_V2:
59+
model_config = ConfigDict(arbitrary_types_allowed=True)
60+
else:
61+
62+
class Config:
63+
arbitrary_types_allowed = True
6064

6165

6266
class UnisegReceipt(SaaReceipt):
@@ -66,8 +70,12 @@ class UnisegReceipt(SaaReceipt):
6670
async def revoke(self):
6771
return await self.data.recall()
6872

69-
class Config:
70-
arbitrary_types_allowed = True
73+
if PYDANTIC_V2:
74+
model_config = ConfigDict(arbitrary_types_allowed=True)
75+
else:
76+
77+
class Config:
78+
arbitrary_types_allowed = True
7179

7280
@property
7381
def raw(self) -> Receipt:

tests/conftest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def pytest_configure(config: pytest.Config):
2424

2525

2626
@pytest.fixture(scope="session", autouse=True)
27-
def load_bot():
27+
def after_nonebot_init(after_nonebot_init: None):
2828
# 加载适配器
2929
driver = nonebot.get_driver()
3030
driver.register_adapter(QQAdapter)
@@ -34,4 +34,5 @@ def load_bot():
3434
driver.register_adapter(SatoriAdapter)
3535

3636
nonebot.require("nonebot_plugin_alconna")
37+
nonebot.require("nonebot_plugin_filehost")
3738
return None

tests/test_target_scope.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ async def test_bots(app: App):
6464
ctx.should_call_api("post_messages", {"channel_id": "789", "msg_id": "", "event_id": None, "content": "test"})
6565
await target3.send(UniMessage("test"))
6666

67+
await asyncio.sleep(0.05)
68+
6769

6870
@pytest.mark.asyncio()
6971
async def test_enable(app: App, mocker: MockerFixture):
@@ -118,3 +120,5 @@ async def test_enable(app: App, mocker: MockerFixture):
118120
driver = get_driver()
119121
driver._bot_connection_hook.clear()
120122
driver._bot_disconnection_hook.clear()
123+
124+
await asyncio.sleep(0.05)

tests/test_uniseg.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import asyncio
2+
13
import pytest
24
from nonebug import App
35
from nonebot import get_adapter
@@ -194,6 +196,7 @@ async def test_uniseg_recv(app: App):
194196
)
195197
msg = await UniMessage.generate(event=event2, bot=bot)
196198
assert msg[UniReply, 0].msg
199+
await asyncio.sleep(0.05)
197200

198201

199202
@pytest.mark.asyncio()
@@ -238,3 +241,4 @@ async def handle(msg: MsgId):
238241
)
239242
target = Target("456", adapter=adapter.get_name())
240243
await target.send("hello!")
244+
await asyncio.sleep(0.05)

tests/test_unmatch.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from typing import Union
2-
31
import pytest
42
from nonebug import App
53
from nonebot import get_adapter
@@ -11,14 +9,12 @@
119

1210
@pytest.mark.asyncio()
1311
async def test_unmatch(app: App):
14-
from nonebot_plugin_alconna import At, Match, UniMessage, on_alconna
12+
from nonebot_plugin_alconna import Match, UniMessage, on_alconna
1513

16-
test_cmd = on_alconna(
17-
Alconna("test", Args["target", Union[int, At]]), skip_for_unmatch=False, auto_send_output=True
18-
)
14+
test_cmd = on_alconna(Alconna("test", Args["target", int]), skip_for_unmatch=False, auto_send_output=True)
1915

2016
@test_cmd.handle()
21-
async def tt_h(target: Match[Union[int, At]]):
17+
async def tt_h(target: Match[int]):
2218
await test_cmd.send(UniMessage(["ok\n", str(target.result)]))
2319

2420
async with app.test_matcher(test_cmd) as ctx:
@@ -36,4 +32,4 @@ async def tt_h(target: Match[Union[int, At]]):
3632
event = fake_group_message_event_v11(message=Message("test abcd"), user_id=123)
3733
ctx.receive_event(bot, event)
3834
ctx.should_not_pass_rule()
39-
ctx.should_call_send(event, "ParamsUnmatched('参数 abcd 不正确')", bot=bot)
35+
ctx.should_call_send(event, "参数 'abcd' 不正确, 其应该符合 'int'", bot=bot)

0 commit comments

Comments
 (0)