Feature: 支持子依赖定义 Pydantic 类型校验 (#2310)

This commit is contained in:
Ju4tCode
2023-08-29 18:45:12 +08:00
committed by GitHub
parent 79f833b946
commit f59271bd47
6 changed files with 244 additions and 25 deletions

View File

@ -1,7 +1,10 @@
from dataclasses import dataclass
from typing_extensions import Annotated
from pydantic import Field
from nonebot import on_message
from nonebot.adapters import Bot
from nonebot.params import Depends
test_depends = on_message()
@ -33,6 +36,14 @@ class ClassDependency:
y: int = Depends(gen_async)
class FooBot(Bot):
...
async def sub_bot(b: FooBot) -> FooBot:
return b
# test parameterless
@test_depends.handle(parameterless=[Depends(parameterless)])
async def depends(x: int = Depends(dependency)):
@ -46,19 +57,46 @@ async def depends_cache(y: int = Depends(dependency, use_cache=True)):
return y
# test class dependency
async def class_depend(c: ClassDependency = Depends()):
return c
# test annotated dependency
async def annotated_depend(x: Annotated[int, Depends(dependency)]):
return x
# test annotated class dependency
async def annotated_class_depend(c: Annotated[ClassDependency, Depends()]):
return c
# test dependency priority
async def annotated_prior_depend(
x: Annotated[int, Depends(lambda: 2)] = Depends(dependency)
):
return x
# test sub dependency type mismatch
async def sub_type_mismatch(b: FooBot = Depends(sub_bot)):
return b
# test type validate
async def validate(x: int = Depends(lambda: "1", validate=True)):
return x
async def validate_fail(x: int = Depends(lambda: "not_number", validate=True)):
return x
# test FieldInfo validate
async def validate_field(x: int = Depends(lambda: "1", validate=Field(gt=0))):
return x
async def validate_field_fail(x: int = Depends(lambda: "0", validate=Field(gt=0))):
return x

View File

@ -42,9 +42,14 @@ async def test_depend(app: App):
ClassDependency,
runned,
depends,
validate,
class_depend,
test_depends,
validate_fail,
validate_field,
annotated_depend,
sub_type_mismatch,
validate_field_fail,
annotated_class_depend,
annotated_prior_depend,
)
@ -62,8 +67,7 @@ async def test_depend(app: App):
event_next = make_fake_event()()
ctx.receive_event(bot, event_next)
assert len(runned) == 2
assert runned[0] == runned[1] == 1
assert runned == [1, 1]
runned.clear()
@ -84,6 +88,29 @@ async def test_depend(app: App):
) as ctx:
ctx.should_return(ClassDependency(x=1, y=2))
with pytest.raises(TypeMisMatch): # noqa: PT012
async with app.test_dependent(
sub_type_mismatch, allow_types=[DependParam, BotParam]
) as ctx:
bot = ctx.create_bot()
ctx.pass_params(bot=bot)
async with app.test_dependent(validate, allow_types=[DependParam]) as ctx:
ctx.should_return(1)
with pytest.raises(TypeMisMatch):
async with app.test_dependent(validate_fail, allow_types=[DependParam]) as ctx:
...
async with app.test_dependent(validate_field, allow_types=[DependParam]) as ctx:
ctx.should_return(1)
with pytest.raises(TypeMisMatch):
async with app.test_dependent(
validate_field_fail, allow_types=[DependParam]
) as ctx:
...
@pytest.mark.asyncio
async def test_bot(app: App):