mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-09-07 04:26:45 +00:00
🐛 fix event maybe converted when checking type (#876)
Fix: 修复 event 类型检查会对类型进行自动转换
This commit is contained in:
@ -1,5 +1,13 @@
|
||||
from nonebot.adapters import Bot
|
||||
|
||||
|
||||
async def get_bot(b: Bot):
|
||||
async def get_bot(b: Bot) -> Bot:
|
||||
return b
|
||||
|
||||
|
||||
class SubBot(Bot):
|
||||
...
|
||||
|
||||
|
||||
async def sub_bot(b: SubBot) -> SubBot:
|
||||
return b
|
||||
|
@ -6,6 +6,14 @@ async def event(e: Event) -> Event:
|
||||
return e
|
||||
|
||||
|
||||
class SubEvent(Event):
|
||||
...
|
||||
|
||||
|
||||
async def sub_event(e: SubEvent) -> SubEvent:
|
||||
return e
|
||||
|
||||
|
||||
async def event_type(t: str = EventType()) -> str:
|
||||
return t
|
||||
|
||||
|
@ -35,11 +35,8 @@ async def test_get(monkeypatch: pytest.MonkeyPatch, nonebug_clear):
|
||||
from nonebot.drivers import ForwardDriver, ReverseDriver
|
||||
from nonebot import get_app, get_bot, get_asgi, get_bots, get_driver
|
||||
|
||||
try:
|
||||
with pytest.raises(ValueError):
|
||||
get_driver()
|
||||
assert False, "Driver can only be got after initialization"
|
||||
except ValueError:
|
||||
assert True
|
||||
|
||||
nonebot.init(driver="nonebot.drivers.fastapi")
|
||||
|
||||
@ -59,11 +56,8 @@ async def test_get(monkeypatch: pytest.MonkeyPatch, nonebug_clear):
|
||||
nonebot.run("arg", kwarg="kwarg")
|
||||
assert runned
|
||||
|
||||
try:
|
||||
with pytest.raises(ValueError):
|
||||
get_bot()
|
||||
assert False
|
||||
except ValueError:
|
||||
assert True
|
||||
|
||||
monkeypatch.setattr(driver, "_clients", {"test": "test"})
|
||||
assert get_bot() == "test"
|
||||
|
@ -36,19 +36,33 @@ async def test_depend(app: App, load_plugin):
|
||||
@pytest.mark.asyncio
|
||||
async def test_bot(app: App, load_plugin):
|
||||
from nonebot.params import BotParam
|
||||
from plugins.param.param_bot import get_bot
|
||||
from nonebot.exception import TypeMisMatch
|
||||
from plugins.param.param_bot import SubBot, get_bot, sub_bot
|
||||
|
||||
async with app.test_dependent(get_bot, allow_types=[BotParam]) as ctx:
|
||||
bot = ctx.create_bot()
|
||||
ctx.pass_params(bot=bot)
|
||||
ctx.should_return(bot)
|
||||
|
||||
async with app.test_dependent(sub_bot, allow_types=[BotParam]) as ctx:
|
||||
bot = ctx.create_bot(base=SubBot)
|
||||
ctx.pass_params(bot=bot)
|
||||
ctx.should_return(bot)
|
||||
|
||||
with pytest.raises(TypeMisMatch):
|
||||
async with app.test_dependent(sub_bot, allow_types=[BotParam]) as ctx:
|
||||
bot = ctx.create_bot()
|
||||
ctx.pass_params(bot=bot)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_event(app: App, load_plugin):
|
||||
from nonebot.exception import TypeMisMatch
|
||||
from nonebot.params import EventParam, DependParam
|
||||
from plugins.param.param_event import (
|
||||
SubEvent,
|
||||
event,
|
||||
sub_event,
|
||||
event_type,
|
||||
event_to_me,
|
||||
event_message,
|
||||
@ -57,11 +71,20 @@ async def test_event(app: App, load_plugin):
|
||||
|
||||
fake_message = make_fake_message()("text")
|
||||
fake_event = make_fake_event(_message=fake_message)()
|
||||
fake_subevent = make_fake_event(_base=SubEvent)()
|
||||
|
||||
async with app.test_dependent(event, allow_types=[EventParam]) as ctx:
|
||||
ctx.pass_params(event=fake_event)
|
||||
ctx.should_return(fake_event)
|
||||
|
||||
async with app.test_dependent(sub_event, allow_types=[EventParam]) as ctx:
|
||||
ctx.pass_params(event=fake_subevent)
|
||||
ctx.should_return(fake_subevent)
|
||||
|
||||
with pytest.raises(TypeMisMatch):
|
||||
async with app.test_dependent(sub_event, allow_types=[EventParam]) as ctx:
|
||||
ctx.pass_params(event=fake_event)
|
||||
|
||||
async with app.test_dependent(
|
||||
event_type, allow_types=[EventParam, DependParam]
|
||||
) as ctx:
|
||||
|
@ -61,6 +61,7 @@ def make_fake_message():
|
||||
|
||||
|
||||
def make_fake_event(
|
||||
_base: Optional[Type["Event"]] = None,
|
||||
_type: str = "message",
|
||||
_name: str = "test",
|
||||
_description: str = "test",
|
||||
@ -72,7 +73,7 @@ def make_fake_event(
|
||||
) -> Type["Event"]:
|
||||
from nonebot.adapters import Event
|
||||
|
||||
_Fake = create_model("_Fake", __base__=Event, **fields)
|
||||
_Fake = create_model("_Fake", __base__=_base or Event, **fields)
|
||||
|
||||
class FakeEvent(_Fake):
|
||||
def get_type(self) -> str:
|
||||
|
Reference in New Issue
Block a user