From f9674da6eabf8ba9e8b23ae0537e2f8cdc9bf22d Mon Sep 17 00:00:00 2001 From: yanyongyu Date: Fri, 21 Jan 2022 21:04:17 +0800 Subject: [PATCH] :bulb: add di docstrings --- nonebot/dependencies/__init__.py | 24 +++++++++++++++++++----- nonebot/dependencies/utils.py | 2 ++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/nonebot/dependencies/__init__.py b/nonebot/dependencies/__init__.py index 49513852..7971f777 100644 --- a/nonebot/dependencies/__init__.py +++ b/nonebot/dependencies/__init__.py @@ -1,8 +1,4 @@ -""" -## 依赖注入处理模块 - -该模块实现了依赖注入的定义与处理。 -""" +"""本模块模块实现了依赖注入的定义与处理。""" import abc import inspect @@ -23,6 +19,11 @@ R = TypeVar("R") class Param(abc.ABC, FieldInfo): + """依赖注入的基本单元 —— 参数。 + + 继承自 `pydantic.fields.FieldInfo`,用于描述参数信息(不包括参数名)。 + """ + @classmethod def _check_param( cls, dependent: "Dependent", name: str, param: inspect.Parameter @@ -45,6 +46,16 @@ class CustomConfig(BaseConfig): class Dependent(Generic[R]): + """依赖注入容器 + + 参数: + call: 依赖注入的可调用对象,可以是任何 Callable 对象 + pre_checkers: 依赖注入解析前的参数检查 + params: 具名参数列表 + parameterless: 匿名参数列表 + allow_types: 允许的参数类型 + """ + def __init__( self, *, @@ -192,3 +203,6 @@ class Dependent(Generic[R]): values[field.name] = value return values + + +__autodoc__ = {"CustomConfig": False} diff --git a/nonebot/dependencies/utils.py b/nonebot/dependencies/utils.py index 56a815ff..1f5c25b4 100644 --- a/nonebot/dependencies/utils.py +++ b/nonebot/dependencies/utils.py @@ -6,6 +6,7 @@ from pydantic.typing import ForwardRef, evaluate_forwardref def get_typed_signature(call: Callable[..., Any]) -> inspect.Signature: + """获取可调用对象签名""" signature = inspect.signature(call) globalns = getattr(call, "__globals__", {}) typed_params = [ @@ -22,6 +23,7 @@ def get_typed_signature(call: Callable[..., Any]) -> inspect.Signature: def get_typed_annotation(param: inspect.Parameter, globalns: Dict[str, Any]) -> Any: + """获取参数的类型注解""" annotation = param.annotation if isinstance(annotation, str): annotation = ForwardRef(annotation)