From 23c237cb2a5517e137892e6377db28a691fdcd27 Mon Sep 17 00:00:00 2001 From: yanyongyu Date: Sun, 21 Nov 2021 17:09:31 +0800 Subject: [PATCH] :bulb: add depends example --- docs/api/dependencies.md | 15 +++++++++++++++ nonebot/dependencies/__init__.py | 14 ++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/docs/api/dependencies.md b/docs/api/dependencies.md index d88aabaa..a93902dc 100644 --- a/docs/api/dependencies.md +++ b/docs/api/dependencies.md @@ -26,3 +26,18 @@ sidebarDepth: 0 * `use_cache: bool = True`: 是否使用缓存。默认为 `True`。 + + +```python +def depend_func() -> Any: + return ... + +def depend_gen_func(): + try: + yield ... + finally: + ... + +async def handler(param_name: Any = Depends(depend_func), gen: Any = Depends(depend_gen_func)): + ... +``` diff --git a/nonebot/dependencies/__init__.py b/nonebot/dependencies/__init__.py index 7d0138a8..eb502e95 100644 --- a/nonebot/dependencies/__init__.py +++ b/nonebot/dependencies/__init__.py @@ -203,5 +203,19 @@ def Depends(dependency: Optional[T_Handler] = None, * ``dependency: Optional[Callable[..., Any]] = None``: 依赖函数。默认为参数的类型注释。 * ``use_cache: bool = True``: 是否使用缓存。默认为 ``True``。 + + .. code-block:: python + + def depend_func() -> Any: + return ... + + def depend_gen_func(): + try: + yield ... + finally: + ... + + async def handler(param_name: Any = Depends(depend_func), gen: Any = Depends(depend_gen_func)): + ... """ return DependsWrapper(dependency=dependency, use_cache=use_cache)