2021-12-20 14:50:11 +08:00
|
|
|
from dataclasses import dataclass
|
2023-03-22 20:54:46 +08:00
|
|
|
from typing_extensions import Annotated
|
2021-12-20 14:50:11 +08:00
|
|
|
|
2023-08-29 18:45:12 +08:00
|
|
|
from pydantic import Field
|
|
|
|
|
2021-12-20 00:28:02 +08:00
|
|
|
from nonebot import on_message
|
2023-08-29 18:45:12 +08:00
|
|
|
from nonebot.adapters import Bot
|
2021-12-20 00:28:02 +08:00
|
|
|
from nonebot.params import Depends
|
|
|
|
|
|
|
|
test_depends = on_message()
|
|
|
|
|
|
|
|
runned = []
|
|
|
|
|
|
|
|
|
|
|
|
def dependency():
|
|
|
|
runned.append(1)
|
|
|
|
return 1
|
|
|
|
|
|
|
|
|
|
|
|
def parameterless():
|
|
|
|
assert len(runned) == 0
|
|
|
|
runned.append(1)
|
|
|
|
|
|
|
|
|
2021-12-20 14:50:11 +08:00
|
|
|
def gen_sync():
|
|
|
|
yield 1
|
|
|
|
|
|
|
|
|
|
|
|
async def gen_async():
|
|
|
|
yield 2
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class ClassDependency:
|
|
|
|
x: int = Depends(gen_sync)
|
|
|
|
y: int = Depends(gen_async)
|
|
|
|
|
|
|
|
|
2023-08-29 18:45:12 +08:00
|
|
|
class FooBot(Bot):
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
|
|
async def sub_bot(b: FooBot) -> FooBot:
|
|
|
|
return b
|
|
|
|
|
|
|
|
|
2021-12-20 00:28:02 +08:00
|
|
|
# test parameterless
|
|
|
|
@test_depends.handle(parameterless=[Depends(parameterless)])
|
|
|
|
async def depends(x: int = Depends(dependency)):
|
|
|
|
# test dependency
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
|
|
@test_depends.handle()
|
|
|
|
async def depends_cache(y: int = Depends(dependency, use_cache=True)):
|
|
|
|
# test cache
|
|
|
|
return y
|
2021-12-20 14:50:11 +08:00
|
|
|
|
|
|
|
|
2023-08-29 18:45:12 +08:00
|
|
|
# test class dependency
|
2021-12-20 14:50:11 +08:00
|
|
|
async def class_depend(c: ClassDependency = Depends()):
|
|
|
|
return c
|
2023-03-22 20:54:46 +08:00
|
|
|
|
|
|
|
|
2023-08-29 18:45:12 +08:00
|
|
|
# test annotated dependency
|
2023-03-22 20:54:46 +08:00
|
|
|
async def annotated_depend(x: Annotated[int, Depends(dependency)]):
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
2023-08-29 18:45:12 +08:00
|
|
|
# test annotated class dependency
|
2023-03-22 20:54:46 +08:00
|
|
|
async def annotated_class_depend(c: Annotated[ClassDependency, Depends()]):
|
|
|
|
return c
|
|
|
|
|
|
|
|
|
2023-08-29 18:45:12 +08:00
|
|
|
# test dependency priority
|
2023-03-22 20:54:46 +08:00
|
|
|
async def annotated_prior_depend(
|
|
|
|
x: Annotated[int, Depends(lambda: 2)] = Depends(dependency)
|
|
|
|
):
|
|
|
|
return x
|
2023-08-29 18:45:12 +08:00
|
|
|
|
|
|
|
|
2023-09-14 00:14:45 +08:00
|
|
|
async def annotated_multi_depend(
|
|
|
|
x: Annotated[Annotated[int, Depends(lambda: 2)], Depends(dependency)]
|
|
|
|
):
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
2023-08-29 18:45:12 +08:00
|
|
|
# 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
|